I have data frame like this:
Column A | Column B |
---|---|
zu | 2.5 |
ab | 1 |
fi | 0 |
zu | 2 |
ab | 0.5 |
fi | 0 |
uv | 1 |
zu | 2 |
ab | 1 |
I need two calculate the difference between "zu" and "ab" and position it on a new column
Column A | Column B | diff |
---|---|---|
zu | 2.5 | 1.5 |
ab | 1 | 0 |
fi | 0 | 0 |
zu | 2 | 1.5 |
ab | 0.5 | 0 |
fi | 0 | 0 |
uv | 1 | 0 |
zu | 2 | 1 |
ab | 1 | 0 |
I tried the diff() and shift() Funktion but that didn't work. Do you have any suggestions?
CodePudding user response:
Example Code
data = {'Column A': {0: 'zu', 1: 'ab', 2: 'fi', 3: 'zu', 4: 'ab', 5: 'fi', 6: 'uv', 7: 'zu', 8: 'ab'},
'Column B': {0: 2.5, 1: 1.0, 2: 0.0, 3: 2.0, 4: 0.5, 5: 0.0, 6: 1.0, 7: 2.0, 8: 1.0}}
df = pd.DataFrame(data)
Code
s = df['Column B'].diff(-1).where(df['Column A'].eq('zu'), 0)
s
0 1.5
1 0.0
2 0.0
3 1.5
4 0.0
5 0.0
6 0.0
7 1.0
8 0.0
make s
to diff column
df.assign(diff=s)
output
Column A Column B diff
0 zu 2.5 1.5
1 ab 1.0 0.0
2 fi 0.0 0.0
3 zu 2.0 1.5
4 ab 0.5 0.0
5 fi 0.0 0.0
6 uv 1.0 0.0
7 zu 2.0 1.0
8 ab 1.0 0.0