Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
创建新列,根据另一列的值是否更改来分配值。
所以我有以下数据框:
我想添加一个名为“trip”的新列,该列以值 1 开头,每次列“orientation”更改值时总和为 1,以便最终数据框如下所示:
我怎么能在 python 中做到这一点?
尝试:
df['trip'] = df['orientation'].ne(df['orientation'].shift()).cumsum()