Получить список заголовков столбцов из фрейма данных Pandas
Опубликовано: 27 Марта, 2022
Let us see how to get all the column headers of a Pandas DataFrame as a list. The df.columns.values attribute will return a list of column headers.
Example 1 :
# importing pandas as pdimport pandas as pd # creating the dataframedf = pd.DataFrame({"PassengerId": [892, 893, 894, 895, 896, 897, 898, 899], "PassengerClass": [1, 1, 2, 1, 3, 3, 2, 2], "PassengerName": ["John", "Prity", "Harry", "Smith", "James", "Amora", "Kiara", "Joseph"], "Age": [32, 54, 71, 21, 37, 9, 11, 54]}) display("The DataFrame :")display(df) # print the list of all the column headersdisplay("The column headers :")display(list(df.columns.values)) |
Output :
Example 2 :
# importing pandas as pdimport pandas as pd # creating the dataframemy_df = {"Students": ["A", "B", "C", "D"], "BMI": [22.7, 18.0, 21.4, 24.1], "Religion": ["Hindu", "Islam", "Christian", "Sikh"]}df = pd.DataFrame(my_df)display("The DataFrame :")display(df) # print the list of all the column headersdisplay("The column headers :")display(list(df.columns.values)) |
Output :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course