Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pivoting DataFrame with multiple columns for the index

I have a dataframe and I want to transpose only few rows to column.

This is what I have now.

   Entity   Name        Date  Value
0     111  Name1  2018-03-31    100
1     111  Name2  2018-02-28    200
2     222  Name3  2018-02-28   1000
3     333  Name1  2018-01-31   2000

I want to create date as the column and then add value. Something like this:

   Entity   Name  2018-01-31  2018-02-28  2018-03-31
0     111  Name1         NaN         NaN       100.0
1     111  Name2         NaN       200.0         NaN
2     222  Name3         NaN      1000.0         NaN
3     333  Name1      2000.0         NaN         NaN

I can have identical Name for two different Entitys. Here is an updated dataset.

Code:

import pandas as pd
import datetime

data1 = {
         'Entity': [111,111,222,333],
         'Name': ['Name1','Name2', 'Name3','Name1'],
         'Date': [datetime.date(2018,3, 31), datetime.date(2018,2,28), datetime.date(2018,2,28), datetime.date(2018,1,31)],
         'Value': [100,200,1000,2000]
    }
df1 = pd.DataFrame(data1, columns= ['Entity','Name','Date', 'Value'])

How do I achieve this? Any pointers? Thanks all.

like image 823
ProgSky Avatar asked Dec 07 '22 15:12

ProgSky


2 Answers

Based on your update, you'd need pivot_table with two index columns -

v = df1.pivot_table(
        index=['Entity', 'Name'], 
         columns='Date', 
         values='Value'
).reset_index()
v.index.name = v.columns.name = None

v
   Entity   Name  2018-01-31  2018-02-28  2018-03-31
0     111  Name1         NaN         NaN       100.0
1     111  Name2         NaN       200.0         NaN
2     222  Name3         NaN      1000.0         NaN
3     333  Name1      2000.0         NaN         NaN
like image 107
cs95 Avatar answered Dec 29 '22 19:12

cs95


From unstack

df1.set_index(['Entity','Name','Date']).Value.unstack().reset_index()

Date  Entity   Name  2018-01-31 00:00:00  2018-02-28 00:00:00  \
0        111  Name1                  NaN                  NaN   
1        111  Name2                  NaN                200.0   
2        222  Name3                  NaN               1000.0   
3        333  Name1               2000.0                  NaN   

Date  2018-03-31 00:00:00  
0                   100.0  
1                     NaN  
2                     NaN  
3                     NaN
like image 33
BENY Avatar answered Dec 29 '22 19:12

BENY