Search code examples
pythonpandasdataframedata-sciencedata-analysis

How could i get pandas groupby not to take indices in account but values of my dataframe


I have a dataFrame with dates and prices, for example :

date price
2006 500
2007 2000
2007 3400
2006 5000

and i want to group my data by year so that i obtain :

2007 2006
2000 500
3400 5000

This is the code i tried : df = my_old_df.groupby(['date']) my_desried_df = pd.DataFrame(data=df.groups)

but i obtain what i desire but with the indices of the values not the value (the price inmy case) i expected.


Solution

  • With pivot :

    out = (
            df.assign(idx= df.groupby("date").cumcount().add(1))
                .pivot(index="idx", columns="date", values="price")
                .rename_axis(None, axis=1).reset_index(drop=True)
    )
    

    Output : ​

    print(out)
    
       2006  2007
    0   500  2000
    1  5000  3400