Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Pivot column to headers

I'm trying to pivot the values in a column to column headers, but maintain the rest of the data. Here's my full code, along with the closest I can get to what I'm looking for. The only problem with this is that I can't figure out how to keep the end column:

import pandas as pd

starts = pd.date_range(start = '1/1/2017', freq = '31d', periods = 4).tolist()
ends = pd.date_range(start = '1/31/2017', freq = '31d', periods = 4).tolist()

df = pd.DataFrame({ 'id':['XXX','XXX','XXX','XXX','YYY','YYY','YYY','YYY'], 
                    'start': starts + starts,
                    'end': ends + ends,
                    'type':['car','car','car','car','truck','truck','truck','truck']
                    }, columns = ['id','start','end','type'])

Original Dataframe:

    id      start        end   type
0  XXX 2017-01-01 2017-01-31    car
1  XXX 2017-02-01 2017-03-03    car
2  XXX 2017-03-04 2017-04-03    car
3  XXX 2017-04-04 2017-05-04    car
4  YYY 2017-01-01 2017-01-31  truck
5  YYY 2017-02-01 2017-03-03  truck
6  YYY 2017-03-04 2017-04-03  truck
7  YYY 2017-04-04 2017-05-04  truck

My closest current pivot attempt:

print df.pivot(index = 'start', columns = 'id', values = 'type').reset_index()

Current output:

id      start  XXX    YYY
0  2017-01-01  car  truck
1  2017-02-01  car  truck
2  2017-03-04  car  truck
3  2017-04-04  car  truck

Desired output:

        start         end  XXX    YYY
0  2017-01-01  2017-01-31  car  truck
1  2017-02-01  2017-03-03  car  truck
2  2017-03-04  2017-04-03  car  truck
3  2017-04-04  2017-05-04  car  truck

I've tried both this and this, with no success.

Any help would be appreciated.

like image 455
elPastor Avatar asked Dec 24 '22 12:12

elPastor


1 Answers

pd.pivot_table(df,index=['start','end'],columns='id',values='type',aggfunc='sum').reset_index()
Out[1587]: 
id       start         end  XXX    YYY
0   2017-01-01  2017-01-31  car  truck
1   2017-02-01  2017-03-03  car  truck
2   2017-03-04  2017-04-03  car  truck
3   2017-04-04  2017-05-04  car  truck
like image 152
BENY Avatar answered Jan 08 '23 04:01

BENY