Python Pandas – 显示 DataFrame 中的所有列名
要仅显示DataFrame中的列名称,请使用dataframe.columns。
使用别名导入所需的库-
import pandas as pd;
以下是数据帧-
dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80]
}
)示例
使用dataframe.columns显示列名。以下是完整的代码-
import pandas as pd;
#创建一个数据框
dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80]
}
)
print"DataFrame ...\n",dataFrame
#获取所有列名
print"\n Display all the column names ...\n",dataFrame.columns输出结果这将产生以下输出-
DataFrame ...
Car Place Units
0 BMW Delhi 100
1 Audi Bangalore 150
2 BMW Hyderabad 50
3 Lexus Chandigarh 110
4 Tesla Pune 90
5 Lexus Mumbai 120
6 Mustang Jaipur 80
Display all the column names ...
Index([u'Car', u'Place', u'Units'], dtype='object')