【问题标题】:Is there a way to skip the first value using itertuples? [duplicate]有没有办法使用 itertuples 跳过第一个值? [复制]
【发布时间】:2019-04-02 04:39:47
【问题描述】:

我是 python 新手。我试图获得前两个值之外的所有值的总和。但我不知道在使用 itertuples 时如何删除第一个值。

我知道我可以删除这些列,但我不想这样做。

Year Population Positive affect Negative affect
2017   10000         5               0
2017   15000         10             15

输出将是

10005
15025

【问题讨论】:

  • df[1:].itertuples() 但更好的答案是:不要使用itertuples
  • @coldspeed 这将省略第一行。

标签: python pandas


【解决方案1】:

在这种情况下,根本不需要迭代。相反,使用矢量化方法sum 以及访问器iloc

df.iloc[:, 1:].sum(axis=1)

输出:

0    10005
1    15025

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-16
    • 2022-09-28
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多