So assume I have a dataframe with rownames that aren't a column of their own per se such as the following:
因此,假设我有一个带有rownames的数据框,这些数据框本身不是它们自己的列,如下所示:
X Y
Row 1 0 5
Row 2 8 1
Row 3 3 0
How would I extract these row names as a list, if I have their index? For example, it would look something like:
如果我有它们的索引,我如何将这些行名称提取为列表?例如,它看起来像:
function_name(dataframe[indices])
> ['Row 1', 'Row 2']
Thanks for your help!
谢谢你的帮助!
4 个解决方案
#1
62
df.index
outputs the row names as pandas Index
object. For a list,
输出行名称为pandas Index对象。对于列表,
list(df.index)
lastly, the index supports label slicing similar to columns
最后,索引支持类似于列的标签切片
df.index['Row 2':'Row 5']
#2
4
If you want to pull out only the index values for certain integer-based row-indices, you can do something like the following using the iloc
method:
如果只想拉出某些基于整数的行索引的索引值,可以使用iloc方法执行以下操作:
In [28]: temp
Out[28]:
index time complete
row_0 2 2014-10-22 01:00:00 0
row_1 3 2014-10-23 14:00:00 0
row_2 4 2014-10-26 08:00:00 0
row_3 5 2014-10-26 10:00:00 0
row_4 6 2014-10-26 11:00:00 0
In [29]: temp.iloc[[0,1,4]].index
Out[29]: Index([u'row_0', u'row_1', u'row_4'], dtype='object')
In [30]: temp.iloc[[0,1,4]].index.tolist()
Out[30]: ['row_0', 'row_1', 'row_4']
#3
3
this seems to work fine :
这似乎工作正常:
dataframe.axes[0].tolist()
dataframe.axes [0] .tolist()
#4
1
if you want to get the index values, you can simply do:
如果你想获得索引值,你可以简单地做:
dataframe.index
dataframe.index
this will output a pandas.core.index
这将输出一个pandas.core.index
#1
62
df.index
outputs the row names as pandas Index
object. For a list,
输出行名称为pandas Index对象。对于列表,
list(df.index)
lastly, the index supports label slicing similar to columns
最后,索引支持类似于列的标签切片
df.index['Row 2':'Row 5']
#2
4
If you want to pull out only the index values for certain integer-based row-indices, you can do something like the following using the iloc
method:
如果只想拉出某些基于整数的行索引的索引值,可以使用iloc方法执行以下操作:
In [28]: temp
Out[28]:
index time complete
row_0 2 2014-10-22 01:00:00 0
row_1 3 2014-10-23 14:00:00 0
row_2 4 2014-10-26 08:00:00 0
row_3 5 2014-10-26 10:00:00 0
row_4 6 2014-10-26 11:00:00 0
In [29]: temp.iloc[[0,1,4]].index
Out[29]: Index([u'row_0', u'row_1', u'row_4'], dtype='object')
In [30]: temp.iloc[[0,1,4]].index.tolist()
Out[30]: ['row_0', 'row_1', 'row_4']
#3
3
this seems to work fine :
这似乎工作正常:
dataframe.axes[0].tolist()
dataframe.axes [0] .tolist()
#4
1
if you want to get the index values, you can simply do:
如果你想获得索引值,你可以简单地做:
dataframe.index
dataframe.index
this will output a pandas.core.index
这将输出一个pandas.core.index