Reputation: 97
I have 2 columns - _a, _b.
import numpy as np
import pandas as pd
df = pd.DataFrame({'_a':[1,1,1,2,2,3,3],'_b':[3,4,5,3,3,3,9]})
df
_a _b
0 1 3
1 1 4
2 1 5
3 2 3
4 2 3
5 3 3
6 3 9
I need change first value in column _b to 0, grouped by column _a Like here:
_a _b
0 1 0
1 1 4
2 1 5
3 2 0
4 2 3
5 3 0
6 3 9
Thank you in advance.
Upvotes: 0
Views: 72
Reputation: 2776
If the first values of each group aren't duplicates, you can use this:
df.loc[df.groupby('_a').head(1).index, '_b'] = 0
output:
_a _b
0 1 0
1 1 4
2 1 5
3 2 0
4 2 3
5 3 0
6 3 9
Upvotes: 2
Reputation: 11602
You can get indices of first occurrences for each value in df._b
with np.unique
:
df._b[np.unique(df._a, return_index=True)[1]] = 0
Upvotes: 0
Reputation:
By default, pd.Series.duplicated
returns False for the first item. Making use of this, you can do:
df.loc[~df['_a'].duplicated(), '_b'] = 0
df
Out:
_a _b
0 1 0
1 1 4
2 1 5
3 2 0
4 2 3
5 3 0
6 3 9
Upvotes: 3