简体   繁体   中英

Dataframe add element from a column based on values contiguity from another columns

I have a df like this:

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'])

Using a I need to add elements of b.

Result I would like:

(1-2)=5+6=11

(10-11)=7+8=15

(15-18)=9+1+2+3=15

(30)=4

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

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?

Using groupby + diff

df['i'].groupby(df['s'].diff().ne(1).cumsum()).sum()

1    11
2    15
3    15
4     4
Name: i, dtype: int64

Another solution:

df.groupby( ((df.s-df.s.shift(1))!=1).cumsum() ).i.sum()

reult:

1    11
2    15
3    15
4     4
Name: i, dtype: int64

You could use NumPy as:

res = []
arr = df.values.copy()
for i in range(1, arr.shape[0]):
    if arr[i, 0] == arr[i-1, 0] + 1:
        arr[i, 1] = arr[i, 1] + arr[i-1, 1]
        
    else:
        res.append(arr[i-1, 1])
res.append(arr[-1, 1])
res

This will give:

[11, 15, 15, 4]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM