获取Pandas DataFrame的行索引值作为列表? [英] Get row-index values of Pandas DataFrame as list?
本文介绍了获取Pandas DataFrame的行索引值作为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在尝试找到此答案时,我可能使用的搜索字词不正确.现在,在索引DataFrame之前,我正在以这种方式在列中获取值列表...
I'm probably using poor search terms when trying to find this answer. Right now, before indexing a DataFrame, I'm getting a list of values in a column this way...
list = list(df['column'])
...然后我将在该列上set_index
.这似乎是浪费的一步.在索引上尝试上述操作时,出现关键错误.
...then I'll set_index
on the column. This seems like a wasted step. When trying the above on an index, I get a key error.
如何获取索引(单索引和多索引)中的值并将它们放在一个或多个元组列表中?
How can I grab the values in an index (both single and multi) and put them in a list or a list of tuples?
推荐答案
要获取index
值作为Index
/MultiIndex
的tuple
的list
/list
,请执行以下操作:>
To get the index
values as a list
/list
of tuple
s for Index
/MultiIndex
do:
df.index.values.tolist() # an ndarray method, you probably shouldn't depend on this
或
list(df.index.values) # this will always work in pandas
这篇关于获取Pandas DataFrame的行索引值作为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文