gpt4 book ai didi

python - 如何比较一列中的字符串值,一一分组

转载 作者:行者123 更新时间:2023-12-04 01:25:25 24 4
gpt4 key购买 nike

我有这样的数据框
我想跟踪更改但在每个 RB 组内,所以我想添加列,如果有更改集 1,否则为 0

enter image description here

最佳答案

您可以创建一个新列作为 更改 对于差异,但首先将它们分组 [RB with value]。

df.sort_values(['RB'], inplace=True)
df['change'] = (df.value != df.value.shift()).astype(int)
print(df)

enter image description here

关于python - 如何比较一列中的字符串值,一一分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62008582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com