I have issue with dataframe. I would like to get the results within starting of the week minus the ending of the week. Is there any way to do it?
Week Number | Position | Desired Results | Operation |
---|---|---|---|
1 | 2 | ||
1 | 2 | ||
1 | 2 | 0 | 2-2 |
2 | 4 | ||
2 | 6 | ||
3 | 7 | 3 | 7-4 |
3 | 12 | ||
3 | 14 | ||
3 | 15 | 3 | 15-12 |
CodePudding user response:
Try groupby
with transform
:
df['Desired'] = df.groupby('Week Number')['Position'].transform(lambda x: x.iloc[-1] - x.iloc[0])
And now:
>>> df
Week Number Position Desired
0 1 2 0
1 1 2 0
2 1 2 0
3 2 4 3
4 2 6 3
5 2 7 3
6 3 12 3
7 3 14 3
8 3 15 3
>>>
Or assign to variable first:
group = df.groupby('Week Number')['Position']
df['Desired'] = group.transform('last') - group.transform('first')
Edit:
To not have duplicates, try:
df['Desired'] = df.groupby('Week Number')['Position'].transform(lambda x: [''] * (len(x) - 1) [x.iloc[-1] - x.iloc[0]])
And now:
>>> df
Week Number Position Desired
0 1 2
1 1 2
2 1 2 0
3 2 4
4 2 6
5 2 7 3
6 3 12
7 3 14
8 3 15 3
>>>