数据框根据其他列的值连续性在该列中添加元素 [英] Dataframe add element from a column based on values contiguity from another columns
本文介绍了数据框根据其他列的值连续性在该列中添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的df:
a=[1,2,10,11,15,16,17,18,30]
b=[5,6,7,8,9,1,2,3,4]
df=pd.DataFrame(list(zip(a,b)),columns=['s','i'])
使用a,我需要添加b的元素.
Using a I need to add elements of b.
我想要的结果:
(1-2)= 5 + 6 = 11
(1-2)=5+6=11
(10-11)= 7 + 8 = 15
(10-11)=7+8=15
(15-18)= 9 + 1 + 2 + 3 = 15
(15-18)=9+1+2+3=15
(30)= 4
我的想法是创建一个连续的值列表,取差值(+1)并用其计算对应的b个元素的总和.
My idea was to create a list of values that are continuous, take the difference(+1) and use it to calculate the sum of the corresponding b elements.
#find continuous integer
def r (nums):
nums= list(df['s'])
gaps = [[s, e] for s, e in zip(nums, nums[1:]) if s+1 < e]
edges = iter(nums[:1] + sum(gaps, []) + nums[-1:])
return (list(zip(edges, edges)))
#difference
a = r(df)
print (a)
for i in range (len(a)):
diff = np.diff(a[i])+1
我正在尝试使用diff作为计数器来添加b的值,但是显然任何一次加法都从第一个值开始.有任何简单的方法可以在不更改b的情况下添加此数字?
I am trying to use diff as a counter to add the value of b but obviously any single time the addition starts from the first value. There is any simple way to add this number without changing b?
推荐答案
使用 groupby
+ diff
df['i'].groupby(df['s'].diff().ne(1).cumsum()).sum()
1 11
2 15
3 15
4 4
Name: i, dtype: int64
这篇关于数据框根据其他列的值连续性在该列中添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文