'Decrement a dataframe column based on a condition
If I have a dataframe like this how can I decrement 'a' based on 'b'?
df = pd.DataFrame([[100,1], [100,2], [100,3], [100,4], [100,5]], columns=["a", "b"])
df.loc[df.b > 2, 'b'] += 1
df.loc[df.b % 2 == 0, 'a'] -= 10
df.loc[df.b % 2 != 0, 'a'] = np.NaN
Desired result:
a b
0 NaN 1
1 90 2
2 80 4
3 NaN 5
4 70 6
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
