Search code examples
pythonpandas-groupbymulti-index

Multiindex Filterings of grouped data


I have a pandas dataframe where I have done a groupby. The groupby results look like this:

enter image description here

As you can see this dataframe has a multilevel index ('ga:dimension3','ga:data') and a single column ('ga:sessions').

I am looking to create a dataframe with the first level of the index ('ga:dimension3') and the first date for each first level index value :

enter image description here

I can't figure out how to do this.

Guidance appreciated.

Thanks in advance.


Solution

  • Inspired from @ggaurav suggestion for using first(), I think that the following should do the work (df is the data you provided, after the group):

    result=df.reset_index(1).groupby('ga:dimension3').first()