1

My input dataframe looks like this:

Variable | fiscal_week_id | units 
xxxxxxxxxxx | 201801 | xx_unit_201801 
xxxxxxxxxxx | 201802 | xx_unit_201802 
xxxxxxxxxxx | 201803 | xx_unit_201803 
yyyyyyyyyyy | 201801 | yy_unit_201801 
yyyyyyyyyyy | 201802 | yy_unit_201802 
yyyyyyyyyyy | 201803 | yy_unit_201803

Need output like this:

Variable | 201801 | 201802 | 201803 
xxxxxxxxxxx | xx_unit_201801 | xx_unit_201802 | xx_unit_201803 
yyyyyyyyyyy | yy_unit_201801 | yy_unit_201802 | yy_unit_201803

Any help please, new to pandas and need solution in pandas only. I tried out with set_index, but didn't seem to work out.

Shaido
  • 25,575
  • 21
  • 68
  • 72

1 Answers1

0

You can just use pd.pivot as @jezrael suggests:

res = df.pivot(index='Variable', columns='fiscal_week_id', values='units')

print(res)

fiscal_week_id          201801          201802          201803
Variable                                                      
xxxxxxxxxxx     xx_unit_201801  xx_unit_201802  xx_unit_201803
yyyyyyyyyyy     yy_unit_201801  yy_unit_201802  yy_unit_201803

Previous attempt:

You can use pd.pivot_table. When you don't have a "real" aggregation function, you can use 'first' to extract the only item:

res = df.pivot_table(index='Variable', columns='fiscal_week_id',
                     values='units', aggfunc='first')
jpp
  • 147,904
  • 31
  • 244
  • 302