python - Pandas: change values in group to minimum -
what have:
df = pd.dataframe({'series1':['a','a','a','a','a','a','b','b','b','b','b','b','b','b','c','c','c','c','c'], 'series2':[1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1], 'series3':[10,12,20,10,12,4,8,8,1,10,12,12,13,13,9,8,7,7,7]}) series1 series2 series3 0 1 10 1 1 12 2 1 20 3 1 10 4 2 12 5 2 4 6 b 1 8 7 b 1 8 8 b 1 1 9 b 1 10 10 b 1 12 11 b 1 12 12 b 1 13 13 b 1 13 14 c 1 9 15 c 1 8 16 c 1 7 17 c 1 7 18 c 1 7
what need group series1 , series2 , convert values in series3 minimum of group. i.e.:
df2 = pd.dataframe({'series1':['a','a','a','a','a','a','b','b','b','b','b','b','b','b','c','c','c','c','c'], 'series2':[1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1], 'series3':[10,10,10,10,4,4,1,1,1,1,1,1,1,1,7,7,7,7,7]}) series1 series2 series3 0 1 10 1 1 10 2 1 10 3 1 10 4 2 4 5 2 4 6 b 1 1 7 b 1 1 8 b 1 1 9 b 1 1 10 b 1 1 11 b 1 1 12 b 1 1 13 b 1 1 14 c 1 7 15 c 1 7 16 c 1 7 17 c 1 7 18 c 1 7
i have feeling can done .groupby(), i'm not sure how replace in existing dataframe, or add new series.
i'm able get:
df.groupby(['series1', 'series2']).min() series3 series1 series2 1 10 2 4 b 1 1 c 1 7
which correct minimums per group, cant figure out simple way pop original dataframe.
you can use groupby.transform
, gives same length series can assign data frame:
df['series3'] = df.groupby(['series1', 'series2']).series3.transform('min') df
Comments
Post a Comment