【问题标题】:Create a new column in a dataframe which adds values with change corresponding to another column在数据框中创建一个新列,该列添加具有与另一列对应的更改的值
【发布时间】:2021-03-11 19:20:22
【问题描述】:

创建新列,根据另一列的值是否更改来分配值。

所以我有以下数据框:

我想添加一个名为“trip”的新列,该列以值 1 开头,每次列“orientation”更改值时总和为 1,以便最终数据框如下所示:

如何在 python 中做到这一点?

【问题讨论】:

    标签: python python-3.x pandas


    【解决方案1】:

    试试:

    df['trip'] = df['orientation'].ne(df['orientation'].shift()).cumsum()
    

    【讨论】:

      猜你喜欢
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多