Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- df = pd.DataFrame({
- 'b': ['s', 'j', 'm', 's', 'j'],
- 'c': [12, 16, 5, 7, 8],
- })
- #print(df)
- df['d'] = pd.np.zeros(df.shape[0])
- #print(df)
- for key, grp in df.groupby('b'):
- #print('---')
- #print(key)
- #print(grp)
- # it calculates first - sum(rest)
- result = grp['c'].iloc[0] - grp['c'].iloc[1:].sum()
- first_index = grp.index[0]
- df['d'][first_index] = result
- #print(key, result)
- print(df)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement