I want to lock the first value from a groupby in dataframe pandas.
Is that possible?
example:
And this is how i would like to have it
Hope someone can help me...
CodePudding user response:
You need to use groupby
transform
.
If you want to replace by the first value:
df['b'] = df.groupby('a')['b'].transform('first')
or by the min value:
df['b'] = df.groupby('a')['b'].transform('min')
output:
a b
0 1 100
1 1 100
2 1 100
3 2 400
4 2 400
5 2 400
6 3 700
7 3 700
8 3 700
CodePudding user response:
This will do it for you
kv = df.groupby(by='first_col')['second_col'].first().to_dict()
def helper(k):
return kv[k]
df['second_col'] = df['first_col'].apply(lambda x: helper(x))
Note first_col
and second_col
are your column names