pandas dataframe groupby index and convert row values into columns

ghukill

While I think I could do this naively and poorly, I'm interested to learn a more elegant and efficient approach.

Given the following dataframe:

In [42]: df = pd.DataFrame({'flavor':['goober','tronic','goober','tronic'], 'points':[42,55,31,101]}, index=['foo','foo','bar','bar'])

In [43]: df
Out[43]: 
     flavor  points
foo  goober      42
foo  tronic      55
bar  goober      31
bar  tronic     101

I would like to groupby the index, and convert values from flavor column into column headers themselves, completely throwing away the flavor and points. So the final result would look like:

In [44]: pd.DataFrame({'goober':[42,31], 'tronic':[55,101]}, index=['foo','bar'])
Out[44]: 
     goober  tronic
foo      42      55
bar      31     101

Thanks for any suggestions.

jezrael

Use DataFrame.pivot with convert index to column first and then remove index and columns names by DataFrame.rename_axis:

df = df.reset_index().pivot('index', 'flavor','points').rename_axis(index=None,columns=None)
print (df)
     goober  tronic
bar      31     101
foo      42      55

Or use DataFrame.set_index with Series.unstack:

df = (df.set_index('flavor', append=True)['points']
        .unstack()
        .rename_axis(index=None, columns=None))
print (df)
     goober  tronic
bar      31     101
foo      42      55

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Get row-index values of Pandas DataFrame as list?

From Java

How to add row in pandas dataframe with None values to some columns

From Java

Pandas Dataframe row selection combined condition index- and column values

From Dev

Construct Pandas DataFrame from dictionary in form {index: list of row values}

From Dev

Converting Pandas groupby.groups result into dataframe, using index tuple value as row and columns name of dataframe

From Dev

Pandas dataframe groupby and combine multiple row values

From Dev

Convert Pandas dataframe to list of list with index, data, and columns

From Dev

Python: How to convert Pandas Dataframe Row Values to individual columns?

From Dev

Find row index of pandas dataframe that don't have finite values

From Dev

Convert row values into columns in R

From Dev

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

From Dev

Getting the index of pandas dataframe for matching row values

From Dev

Summing values from pandas dataframe columns depending on row index value

From Dev

Convert Pandas groupby group into columns

From Dev

pandas dataframe: is there any way to transform columns as row values in pandas

From Dev

Pandas Dataframe row selection combined condition index- and column values

From Dev

Pandas: Select values from specific columns of a DataFrame by row

From Dev

Convert pandas columns values to row

From Dev

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

From Dev

Summing values from pandas dataframe columns depending on row index value

From Dev

pandas series or tidy dataframe: index level values to dataframe columns

From Dev

Set values for particular columns in a row of a Pandas Dataframe

From Dev

Convert pandas dataframe to multi-index columns

From Dev

How to convert groupby multi-index as a new columns in Pandas?

From Dev

convert index into columns - pandas

From Dev

Pandas dataframe - Rearranging row index values into column headers

From Dev

Two dataframe columns to row and column index with third column as values

From Dev

Pandas Dataframe Groupby multiple columns

From Dev

How to get row index of pandas dataframe containing specific values from two columns?

Related Related

  1. 1

    Get row-index values of Pandas DataFrame as list?

  2. 2

    How to add row in pandas dataframe with None values to some columns

  3. 3

    Pandas Dataframe row selection combined condition index- and column values

  4. 4

    Construct Pandas DataFrame from dictionary in form {index: list of row values}

  5. 5

    Converting Pandas groupby.groups result into dataframe, using index tuple value as row and columns name of dataframe

  6. 6

    Pandas dataframe groupby and combine multiple row values

  7. 7

    Convert Pandas dataframe to list of list with index, data, and columns

  8. 8

    Python: How to convert Pandas Dataframe Row Values to individual columns?

  9. 9

    Find row index of pandas dataframe that don't have finite values

  10. 10

    Convert row values into columns in R

  11. 11

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

  12. 12

    Getting the index of pandas dataframe for matching row values

  13. 13

    Summing values from pandas dataframe columns depending on row index value

  14. 14

    Convert Pandas groupby group into columns

  15. 15

    pandas dataframe: is there any way to transform columns as row values in pandas

  16. 16

    Pandas Dataframe row selection combined condition index- and column values

  17. 17

    Pandas: Select values from specific columns of a DataFrame by row

  18. 18

    Convert pandas columns values to row

  19. 19

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

  20. 20

    Summing values from pandas dataframe columns depending on row index value

  21. 21

    pandas series or tidy dataframe: index level values to dataframe columns

  22. 22

    Set values for particular columns in a row of a Pandas Dataframe

  23. 23

    Convert pandas dataframe to multi-index columns

  24. 24

    How to convert groupby multi-index as a new columns in Pandas?

  25. 25

    convert index into columns - pandas

  26. 26

    Pandas dataframe - Rearranging row index values into column headers

  27. 27

    Two dataframe columns to row and column index with third column as values

  28. 28

    Pandas Dataframe Groupby multiple columns

  29. 29

    How to get row index of pandas dataframe containing specific values from two columns?

HotTag

Archive