Get list from pandas DataFrame column headers
我想从熊猫数据帧中获取列标题的列表。数据帧将来自用户输入,因此我不知道将有多少列,或者将如何调用它们。
例如,如果给我这样的数据帧:
| 1 2 3 4 5 6 7 8 9 10 11 12 | >>> 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 | 
我想得到如下列表:
| 1 2 | >>> header_list [y, gdp, cap] | 
您可以通过执行以下操作以列表形式获取值:
| 1 | list(my_dataframe.columns.values) | 
也可以简单地使用:(如Ed Chum的答案所示):
| 1 | list(my_dataframe) | 
有一种内置方法是最有效的:
| 1 | my_dataframe.columns.values.tolist() | 
如果性能对您不那么重要,那么
| 1 | my_dataframe.columns.tolist() | 
性能差异明显:
| 1 2 3 4 5 | %timeit df.columns.tolist() 16.7 μs ± 317 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) %timeit df.columns.values.tolist() 1.24 μs ± 12.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) | 
对于那些讨厌打字的人,你可以在
| 1 | list(df) | 
做了一些快速的测试,也许毫不奇怪,使用
| 1 2 3 4 5 6 7 8 9 10 11 | In [1]: %timeit [column for column in df] 1000 loops, best of 3: 81.6 μs per loop In [2]: %timeit df.columns.values.tolist() 10000 loops, best of 3: 16.1 μs per loop In [3]: %timeit list(df) 10000 loops, best of 3: 44.9 μs per loop In [4]: % timeit list(df.columns.values) 10000 loops, best of 3: 38.4 μs per loop | 
(不过,我还是很喜欢
它变得更简单(熊猫0.16.0):
| 1 | df.columns.tolist() | 
将在一个好的列表中为您提供列名称。
| 1 2 | >>> list(my_dataframe) ['y', 'gdp', 'cap'] | 
要在调试器模式下列出数据帧的列,请使用列表理解:
| 1 2 | >>> [c for c in my_dataframe] ['y', 'gdp', 'cap'] | 
另外,只需使用
| 1 2 | >>> sorted(my_dataframe) ['cap', 'gdp', 'y'] | 
这是以
有趣的是,
| 1 2 3 4 5 | In [97]: %timeit df.columns.values.tolist() 100000 loops, best of 3: 2.97 μs per loop In [98]: %timeit df.columns.tolist() 10000 loops, best of 3: 9.67 μs per loop | 
数据帧遵循类似dict的约定,迭代对象的"键"。
| 1 | my_dataframe.keys() | 
创建键/列列表-对象方法
| 1 2 | my_dataframe.keys().to_list() list(my_dataframe.keys()) | 
数据帧上的基本迭代返回列标签
| 1 | [column for column in my_dataframe] | 
不要将数据帧转换为列表,只需要获取列标签。在寻找方便的代码示例时不要停止思考。
| 1 2 3 | xlarge = pd.DataFrame(np.arange(100000000).reshape(10000,10000)) list(xlarge) #compute time and memory consumption depend on dataframe size - O(N) list(xlarge.keys()) #constant time operation - O(1) | 
在笔记本上
对于ipython笔记本中的数据探索,我的首选方法是:
| 1 | sorted(df) | 
它将产生一个易于阅读的按字母顺序排列的列表。
在代码存储库中在代码中,我发现这样做更明确
| 1 | df.columns | 
因为它告诉别人你在读你的代码。
正如西蒙·维瑟的回答……你可以的。
| 1 | list(my_dataframe.columns.values) | 
或
| 1 | list(my_dataframe) # for less typing. | 
但我认为最美妙的地方是:
| 1 | list(my_dataframe.columns) | 
它是明确的,同时也不是不必要的长。
这为我们提供了列表中列的名称:
| 1 | list(my_dataframe.columns) | 
也可以使用另一个名为tolist()的函数:
| 1 | my_dataframe.columns.tolist() | 
我觉得这个问题值得进一步解释。
正如@fixxer所指出的,答案取决于您在项目中使用的熊猫版本。你可以用
如果您出于某种原因(在Debian Jessie上,我使用0.14.1)使用的是比0.16.0更旧版本的熊猫,那么您需要使用:
这种键方法的优点是,它甚至在更新版本的熊猫中也能工作,所以它更通用。
要快速、整洁、目视检查,请尝试以下操作:
| 1 2 | for col in df.columns: print col | 
| 1 2 3 4 | n = [] for i in my_dataframe.columns: n.append(i) print n | 
很惊讶我到目前为止还没有看到这个帖子,所以我就把它留在这里。
扩展的Iterable解包(python3.5+):python 3.5引入了解包通用化(pep 448)。因此,以下操作都是可能的。
| 1 2 3 4 5 6 7 8 9 | df = pd.DataFrame('x', columns=['A', 'B', 'C'], index=range(5)) df A B C 0 x x x 1 x x x 2 x x x 3 x x x 4 x x x | 
如果你想要一个
| 1 2 | [*df] # ['A', 'B', 'C'] | 
或者,如果你想要一个
| 1 2 | {*df} # {'A', 'B', 'C'} | 
或者,如果你想要一个
| 1 2 | *df,  # Please note the trailing comma # ('A', 'B', 'C') | 
或者,如果你想把结果存储在某个地方,
| 1 2 3 | *cols, = df  # A wild comma appears, again cols # ['A', 'B', 'C'] | 
…如果你是那种把咖啡转换成打字声音的人,那么,这会更有效地消耗你的咖啡;)
P.S.: if performance is important, you will want to ditch the
solutions above in favour of
2
# ['A', 'B', 'C']This is similar to Ed Chum's
answer, but updated for
v0.24 where.to_numpy() is preferred to the use of.values . See
this answer (by me)
for more information.
目视检查因为我已经在其他答案中看到了这一点,所以您可以使用ITerable解包(不需要显式循环)。
| 1 2 3 4 5 6 7 8 | print(*df) A B C print(*df, sep=' ') A B C | 
对其他方法的批评
对于可以在一行中完成的操作,不要使用显式的
其次,使用
接下来,
最后,列表化即
| 1 2 3 | %%timeit final_df.columns.values.tolist() 948 ns ± 19.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) | 
| 1 2 3 | %%timeit list(final_df.columns) 14.2 μs ± 79.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) | 
| 1 2 3 | %%timeit list(final_df.columns.values) 1.88 μs ± 11.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) | 
| 1 2 3 | %%timeit final_df.columns.tolist() 12.3 μs ± 27.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) | 
| 1 2 3 | %%timeit list(final_df.head(1).columns) 163 μs ± 20.6 μs per loop (mean ± std. dev. of 7 runs, 10000 loops each) | 
此解决方案列出对象"我的数据框"的所有列:
| 1 | print(list(my_dataframe)) | 
尽管上面提供的解决方案很好。我还希望像frame.column_names()这样的函数在pandas中是一个函数,但由于它不是函数,所以使用下面的语法可能会更好。它通过调用"tolist"函数:frame.columns.tolist(),以某种方式保留了您使用熊猫的感觉。
| 1 | frame.columns.tolist() |