Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding level =0 and group_keys

I am using pandas groupby and finding size for ex:

dd=df.groupby(['value','year','team']).size()

and it giving me output as:

value  year  team
0      2000  B       2
1      2000  A       2
       2001  A       1
2      2001  B       1
3      2001  A       2

my question is what is level =0 and group_keys (given below )which is applying on grouped dataframe dd.

ddf3=dd.groupby(level=0,group_keys=False).apply(function).reset_index()

is (level=0) be 'value' column in grouped dataframe dd.

Please help me.

like image 998
user9116565 Avatar asked Apr 16 '18 14:04

user9116565


People also ask

What is Group_keys in Groupby pandas?

group_keys parameter in groupby comes handy during apply operations that creates an additional index column corresponding to the grouped columns[ group_keys=True ] and eliminates in the case[ group_keys=False ] especially during the case when trying to perform operations on individual columns.

What is level in group by?

The level in groupby() is used when you have multiple indices and you want to use only one index of the DataFrame. For example: df = pd.DataFrame([{'values':0,'year':2000,'team':'A'}, {'values':1,'year':2000,'team':'B'}, {'values':2,'year':2001,'team':'B'} ]) df = df.groupby(['values','year','team']).size() df.

How do I group values in a column in pandas?

Groupby is a very powerful pandas method. You can group by one column and count the values of another column per this column value using value_counts. Using groupby and value_counts we can count the number of activities each person did.


2 Answers

df.groupby(level=0)

It specifies the first index of the Dataframe. When you have multiple indices and you need to groupby only one index of those multiple indices of the dataframe we use it.

It means:

  • level 0 -> First Index
  • level 1 -> Second Index
  • etc..
like image 80
Raviteja Avatar answered Sep 19 '22 03:09

Raviteja


The level in groupby() is used when you have multiple indices and you want to use only one index of the DataFrame. For example:

df = pd.DataFrame([{'values':0,'year':2000,'team':'A'}, 
                   {'values':1,'year':2000,'team':'B'}, 
                   {'values':2,'year':2001,'team':'B'}
                  ])
df = df.groupby(['values','year','team']).size()
df

Output:

values  year  team
0       2000  A       1
1       2000  B       1
2       2001  B       1
df = df.groupby(level=1).size()
df

Output:

year
2000    2
2001    1
like image 29
Harshitha S V Avatar answered Sep 20 '22 03:09

Harshitha S V