获取 Pandas DataFrame 的行索引值作为列表? [英] Get row-index values of Pandas DataFrame as list?

查看:77
本文介绍了获取 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值作为list/list的<Index/MultiIndex 的代码>元组s 做:

To get the index values as a list/list of tuples 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屋!

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