Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

python - 从pandas DataFrame列标题获取列表(Get list from pandas DataFrame column headers)

I want to get a list of the column headers from a pandas DataFrame.

(我想从pandas DataFrame获取列标题的列表。)

The DataFrame will come from user input so I won't know how many columns there will be or what they will be called.

(DataFrame来自用户输入,所以我不知道会有多少列或它们将被称为什么。)

For example, if I'm given a DataFrame like this:

(例如,如果给我这样的DataFrame:)

>>> my_dataframe
    y  gdp  cap
0   1    2    5
1   2    3    9
2   8    7    2
3   3    4    7
4   6    7    7
5   4    8    3
6   8    2    8
7   9    9   10
8   6    6    4
9  10   10    7

I would want to get a list like this:

(我想要一个这样的列表:)

>>> header_list
['y', 'gdp', 'cap']
  ask by natsuki_2002 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can get the values as a list by doing:

(您可以执行以下操作以列表形式获取值:)

list(my_dataframe.columns.values)

Also you can simply use: (as shown in Ed Chum's answer ):

(您也可以简单地使用:(如Ed Chum的答案所示 ):)

list(my_dataframe)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...