【问题标题】:How to get a list from a Pandas' series which includes the index of the row [duplicate]如何从熊猫系列中获取包含行索引的列表[重复]
【发布时间】:2019-09-24 07:04:56
【问题描述】:

我有一个 Pandas DataFrame(从 excel 文件创建),我想在其中查看一行,忽略 nan 值,并获取剩余元素的位置。所以我在做:

lines = df.iloc[16, :].dropna()
print(lines)

产量:

2      10
6      11
10     12
14     13
18     14

第一列是 Pandas 为我的 excel 中的每一行提供的自动索引。我想要列出这些索引,position = [2, 6, 10, 14, 18]

如何提取这些索引?我尝试使用lines.tolist(),但它只获取没有位置/索引的值。

【问题讨论】:

  • lines.index.tolist()
  • df.iloc[16, :].dropna().index.tolist()

标签: python excel pandas


【解决方案1】:

尝试使用以下行:

lines.index.tolist()

或者list(lines.index) 也可以工作

【讨论】:

    猜你喜欢
    • 2020-12-10
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多