measure_theory
measure_theory

Reputation: 874

Pandas: change values in group to minimum

What I 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        A        1       10
1        A        1       12
2        A        1       20
3        A        1       10
4        A        2       12
5        A        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 I need is to group by SERIES1 and SERIES2 and to convert the values in SERIES3 to the minimum of that 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        A        1       10
1        A        1       10
2        A        1       10
3        A        1       10
4        A        2        4
5        A        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 a feeling this can be done with .groupby(), but I'm not sure how to replace it in the existing DataFrame, or to add it as new series.

I'm able to get:

df.groupby(['SERIES1', 'SERIES2']).min() 

                 SERIES3
SERIES1 SERIES2         
A       1             10
        2              4
B       1              1
C       1              7

which are the correct minimums per group, but I cant figure out a simple way to pop that back into the original dataframe.

Upvotes: 0

Views: 654

Answers (1)

akuiper
akuiper

Reputation: 214957

You can use groupby.transform, which gives back a same length series that you can assign back to the data frame:

df['SERIES3'] = df.groupby(['SERIES1', 'SERIES2']).SERIES3.transform('min')
df

enter image description here

Upvotes: 1

Related Questions