📜  从 Pandas DataFrame 中获取列标题列表

📅  最后修改于: 2022-05-13 01:54:33.008000             🧑  作者: Mango

从 Pandas DataFrame 中获取列标题列表

让我们看看如何将 Pandas DataFrame 的所有列标题作为列表获取。 df.columns.values属性将返回列标题列表。

示例 1:

# importing pandas as pd
import pandas as pd
  
# creating the dataframe
df = 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 headers
display("The column headers :")
display(list(df.columns.values))

输出 :

示例 2:

# importing pandas as pd
import pandas as pd
  
# creating the dataframe
my_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 headers
display("The column headers :")
display(list(df.columns.values))

输出 :