0

I have the following dataframe:

Date             Rez          ID
                            
2023-07-03       5.95          1
2023-07-03       49.9          3
2023-07-03      33.17         54
2022-11-17       5.38          1
2022-11-17       44.4          3
2022-09-02      23.39         54
2022-09-02        5.6          1
2022-09-02       46.5          3
2021-10-19       5.34          1
2021-10-19       44.6          3
2020-12-11       5.38          1
2020-12-11         44          3
2019-04-25       5.84          1
2019-04-25        1.7        205

And would like to obtain something like this (with "ID" values as the columns and "Date" as the index)

                  1            3          54        205
Date                                  
2023-07-03       5.95         49.9       33.17      NaN        
2022-11-17       5.38         44.4       NaN        NaN        
2022-09-02        5.6         46.5       23.39      NaN        
2021-10-19       5.34         44.6       NaN        NaN        
2020-12-11       5.38         44         NaN        NaN        
2019-04-25       5.84         NaN        NaN        1.7

I tried the following, but I don't know how to go further (i don't get what I need):

df_sample.groupby(['ID','Date'])['Rez'].unique().to_frame().transpose()

static
  • 3
  • 3

2 Answers2

0

Pivot the DataFrame

pivot_df = df.pivot_table(index='Date', columns='ID', values='Rez', aggfunc=list)

Display the result

print(pivot_df)

Adnan
  • 1
  • 2
0

Use,

df.pivot(index='Date', columns='ID', values='Rez')

Output:

ID           1     3      54   205
Date                              
2019-04-25  5.84   NaN    NaN  1.7
2020-12-11  5.38  44.0    NaN  NaN
2021-10-19  5.34  44.6    NaN  NaN
2022-09-02  5.60  46.5  23.39  NaN
2022-11-17  5.38  44.4    NaN  NaN
2023-07-03  5.95  49.9  33.17  NaN
Scott Boston
  • 147,308
  • 15
  • 139
  • 187