【问题标题】:Return index value of particular row in python dataframe OR list返回python数据框或列表中特定行的索引值
【发布时间】:2018-01-22 10:43:11
【问题描述】:

简单的索引问题。 我有以下名为 xlist 的列表对象。 也可以是df。

[        result
 6221  0.974214
 6220  0.973909
 6222  0.973447
 3032  0.973444
 3033  0.973387]

我想获取特定行的索引“值”(或者它是作为列表的列?)。例如:我想指定第 2 行并得到 6222。

我很困惑为什么这并不简单(无论如何对我来说)。

【问题讨论】:

  • I have the following list object named as xlist. It could also be a df. 它看起来像一个列表,里面有一个数据框。抱歉,您的问题有点不清楚。
  • 添加实际输入结构:1)作为列表; 2)作为数据框
  • 谢谢。道歉。这是一个清单。但我的问题同样与 DataFrame 有关。我不明白为什么获取索引值对他们来说应该不同。这是输入结构:xlist = [] xlist.append(resdf.nlargest(5,'result'))

标签: python indexing


【解决方案1】:

如果你有一个 DataFrames 列表,你可以这样做:

#for the first DataFrame in list and the numbers represent the index in those DataFrames
needed_value = xlist[0].reset_index().iloc[1]['index'] #iloc[1] to select 2nd row

print(needed_value)
6222

【讨论】:

  • 谢谢你的作品。显得非常复杂。我想我应该首先避免将 df 元素附加到列表中。
猜你喜欢
  • 2021-12-17
  • 2014-01-27
  • 2019-09-16
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 2021-01-05
相关资源
最近更新 更多