简体   繁体   中英

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.

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

Using set_index and unstack,

df.set_index(['start', 'end', 'id']).type.unstack().reset_index()



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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM