gpt4 book ai didi

python - 连续相同值的总和

转载 作者:行者123 更新时间:2023-11-28 22:37:47 25 4
gpt4 key购买 nike

如何获得 pandas 系列中连续 1 的总和?

例如,s = pd.Series([5, 1, 4, 1, 1, 2, 3, 1, 1, 1, 4])。我想获得 pd.Series([0, 1, 0, 1, 2, 0, 0, 1, 2, 3, 0])

( Pandas 0.18.0)

最佳答案

你可以试试groupbycumcounts1 != 1cumsum 进行比较:

print s1.groupby((s1 != 1).cumsum()).cumcount()
0 0
1 1
2 0
3 1
4 2
5 0
6 0
7 1
8 2
9 3
10 0
dtype: int64

更好的解释:

df = pd.DataFrame(s1, columns=['orig'])
df['not1'] = s1 != 1
df['cumsum'] = (s1 != 1).cumsum()
df['cumcount'] = s1.groupby((s1 != 1).cumsum()).cumcount()
#s1.groupby((s1 != 1).cumsum()).cumcount() is same as:
df['cumcount1'] = df.groupby('cumsum')['orig'].cumcount()
print df
orig not1 cumsum cumcount cumcount1
0 5 True 1 0 0
1 1 False 1 1 1
2 3 True 2 0 0
3 4 True 3 0 0
4 1 False 3 1 1
5 1 False 3 2 2
6 2 True 4 0 0
7 3 True 5 0 0
8 1 False 5 1 1
9 1 False 5 2 2
10 1 False 5 3 3
11 4 True 6 0 0

或者:

print (s1 == 1) * (s1.groupby((s1 != s1.shift()).cumsum()).cumcount() + 1)
0 0
1 1
2 0
3 1
4 2
5 0
6 0
7 1
8 2
9 3
10 0
dtype: int64

解释:

df = pd.DataFrame(s1, columns=['orig'])
df['compare_shift'] = s1 != s1.shift()
df['cumsum'] = (s1 != s1.shift()).cumsum()
df['cumcount'] = s1.groupby((s1 != s1.shift()).cumsum()).cumcount() + 1
df['cumcount1'] = df.groupby('cumsum')['orig'].cumcount() + 1
df['is1'] = (s1 == 1)
#True in converted to 1, False to 0
df['fin'] = (s1 == 1) * (s1.groupby((s1 != s1.shift()).cumsum()).cumcount() + 1)
print df
orig compare_shift cumsum cumcount cumcount1 is1 fin
0 5 True 1 1 1 False 0
1 1 True 2 1 1 True 1
2 3 True 3 1 1 False 0
3 4 True 4 1 1 False 0
4 1 True 5 1 1 True 1
5 1 False 5 2 2 True 2
6 2 True 6 1 1 False 0
7 3 True 7 1 1 False 0
8 1 True 8 1 1 True 1
9 1 False 8 2 2 True 2
10 1 False 8 3 3 True 3
11 4 True 9 1 1 False 0

关于python - 连续相同值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36231020/

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