0 votes
1 view
in Data Science by (17.6k points)

I group a pandas dataframe using groupby() function with multiple columns.

df_tr_mod = df_tr.groupby(['Col1','Col2']).aCol.agg(['count'])

Now I want to access this count values (I want to multiply this all count values by 10) How i can do this?

1 Answer

0 votes
by (38.2k points)

Here, use  GroupBy.size :  

df_tr = pd.DataFrame({'Col1':[1,2,1,2,2],

                      'Col2':[5,5,5,6,6],

                      'aCol':[1,8,9,6,4]})

print(df_tr)

   Col1  Col2  aCol

0     1     5     1

1     2     5     8

2     1     5     9

3     2     6     6

4     2     6     4

# multiply 10 in you solution

df_tr_mod = df_tr.groupby(['Col1','Col2']).aCol.agg(['count']) * 10

print (df_tr_mod)

           count

Col1 Col2       

1    5        20

2    5        10

     6        20

print (type(df_tr_mod))

<class 'pandas.core.frame.DataFrame'>

#For MultiIndex, add to_frame

df_tr_mod = df_tr.groupby(['Col1','Col2']).size().to_frame(name='count') * 10

print (df_tr_mod)

           count

Col1 Col2       

1    5        20

2    5        10

     6        20

#Add reset_index() for all columns from index 

df_tr_mod = df_tr.groupby(['Col1','Col2']).size().reset_index(name='count') 

df_tr_mod["count"]= df_tr_mod["count"]*10

print (df_tr_mod)

   Col1  Col2  count

0     1     5     20

1     2     5     10

2     2     6     20

Using agg function:

df_tr_mod = df_tr.groupby(['Col1','Col2']).aCol.agg(['size', 'sum', 'mean'])

print (df_tr_mod)

           size  sum  mean

Col1 Col2                 

1    5        2   10     5

2    5        1    8     8

     6        2   10     5

If you wish to Learn about Python visit this Python Tutorial.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 31, 2019 in Data Science by sourav (17.6k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...