从 Pandas DataFrame 列标题获取列表 [英] Get a list from Pandas DataFrame column headers

查看:47
本文介绍了从 Pandas DataFrame 列标题获取列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Pandas DataFrame 中获取列标题列表.DataFrame 将来自用户输入,所以我不知道会有多少列或它们将被称为什么.

例如,如果给我一个这样的 DataFrame:

<预><代码>>>>my_dataframey GDP 上限0 1 2 51 2 3 92 8 7 23 3 4 74 6 7 75 4 8 36 8 2 87 9 9 108 6 6 49 10 10 7

我会得到一个这样的列表:

<预><代码>>>>标题列表['y', 'gdp', 'cap']

解决方案

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

list(my_dataframe.columns.values)

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

list(my_dataframe)

I want to get a list of the column headers from a 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.

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

>>> 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 get a list like this:

>>> header_list
['y', 'gdp', 'cap']

解决方案

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):

list(my_dataframe)

这篇关于从 Pandas DataFrame 列标题获取列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆