How to access MultiIndex column after groupby in pandas?

polyglot

With a single-indexed dataframe, the columns are available in the group by object:

df1 = pd.DataFrame({'a':[2,2,4,4], 'b': [5,6,7,8]})
df1.groupby('a')['b'].sum() -> 

a
2    11
4    15

But in a MultiIndex dataframe when not grouping by level, the columns are no longer accessible in the group by object

df = pd.concat([df1, df1], keys=['c', 'd'], axis=1)
df -> 

   c     d
   a  b  a  b
0  2  5  2  5
1  2  6  2  6
2  4  7  4  7
3  4  8  4  8

df.groupby([('c','a')])[('c','b')].sum() -> 
KeyError: "Columns not found: 'b', 'c'"

As a workaround, this works but it's not efficient since it doesn't use the cpythonized aggregator, not to mention it's awkward looking.

df.groupby([('c','a')]).apply(lambda df: df[('c', 'b')].sum())

Is there a way to access MultiIndex column in groupby object that I missed?

root

Adding a comma after your ('c','b') tuple seems to work:

df.groupby([('c','a')])[('c','b'),].sum()

I'm guessing that without the comma, pandas is just interpreting them as separate items.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access column in groupby? - Pandas

From Dev

How to access column in groupby? - Pandas

From Dev

How do I change or access pandas MultiIndex column headers?

From Dev

pandas, how to access multiIndex dataframe?

From Dev

How to access multiindex levels in pandas?

From Dev

missing column after pandas groupby

From Dev

missing column after pandas groupby

From Dev

How to move pandas data from index to column after multiple groupby

From Dev

Accessing columns with MultiIndex after using pandas groupby and aggregate

From Dev

How to get Pandas column multiindex names as a list

From Dev

How to check if a column exists in a pandas MultiIndex

From Dev

Pandas groupby columns without multiindex

From Dev

Apply pandas groupby aggregation on multiindex

From Dev

How to access pandas multiindex by element within a nested dict?

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

How to move pandas data frame multiindex column into 2 rows

From Dev

pandas how do I retrieve the index column of multiIndex

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

How to get Pandas column multiindex FULL name as a list

From Dev

After groupby, how to flatten column headers?

From Java

How to show only column with Values in Pandas Groupby

From Dev

How to groupby an index as well as a column in pandas

From Dev

Pandas: how to get a particular group after groupby?

From Dev

How to plot aggregate results after groupby in Pandas?

From Dev

How to plot aggregate results after groupby in Pandas?

From Dev

Idiomatic multiindex column assignment in pandas

From Dev

Remove column from Pandas multiindex

From Dev

Python/Pandas - Query a MultiIndex Column

From Dev

convert pandas column of tuples to MultiIndex

Related Related

  1. 1

    How to access column in groupby? - Pandas

  2. 2

    How to access column in groupby? - Pandas

  3. 3

    How do I change or access pandas MultiIndex column headers?

  4. 4

    pandas, how to access multiIndex dataframe?

  5. 5

    How to access multiindex levels in pandas?

  6. 6

    missing column after pandas groupby

  7. 7

    missing column after pandas groupby

  8. 8

    How to move pandas data from index to column after multiple groupby

  9. 9

    Accessing columns with MultiIndex after using pandas groupby and aggregate

  10. 10

    How to get Pandas column multiindex names as a list

  11. 11

    How to check if a column exists in a pandas MultiIndex

  12. 12

    Pandas groupby columns without multiindex

  13. 13

    Apply pandas groupby aggregation on multiindex

  14. 14

    How to access pandas multiindex by element within a nested dict?

  15. 15

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  16. 16

    How to move pandas data frame multiindex column into 2 rows

  17. 17

    pandas how do I retrieve the index column of multiIndex

  18. 18

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  19. 19

    How to get Pandas column multiindex FULL name as a list

  20. 20

    After groupby, how to flatten column headers?

  21. 21

    How to show only column with Values in Pandas Groupby

  22. 22

    How to groupby an index as well as a column in pandas

  23. 23

    Pandas: how to get a particular group after groupby?

  24. 24

    How to plot aggregate results after groupby in Pandas?

  25. 25

    How to plot aggregate results after groupby in Pandas?

  26. 26

    Idiomatic multiindex column assignment in pandas

  27. 27

    Remove column from Pandas multiindex

  28. 28

    Python/Pandas - Query a MultiIndex Column

  29. 29

    convert pandas column of tuples to MultiIndex

HotTag

Archive