简体   繁体   中英

Pivot a Pandas dataframe using multiple columns

This is a follow up question to Pivot a dataframe with two columns as the index .

My data is in this format:

Record ID Para  Col2     Col3
1          A        x      a
1          A        x      b
2          B        y      a
2          B        y      b
1          A        z      c
1          C        x      a

I would like to reshape it into:

Record Para  a     b      c    x   y  z 
1       A    1     1      1    1   0  1
1       C    1     1      1    1   0  1
2       B    1     1      0    0   1  0 

I tried

    csv3 = csv2.pivot_table(index=['Record ID', 'Para'], columns=csv2.iloc[:,2:], aggfunc='size', fill_value=0).reset_index()

but don't get the columns right. What do I need to do differently?

UPDATE 1:

I have 10s of columns.

You can aggregate to set and then use get_dummies .

df2 = df.groupby(['RecordID', 'Para'])[df.columns[2:]].aggregate(set)

values = df2.apply(lambda x: set().union(*x.values), axis=1)
dummies = values.str.join('|').str.get_dummies()

res = dummies.reset_index()

print(res)

   RecordID Para  a  b  c  x  y  z
0         1    A  1  1  1  1  0  1
1         2    B  1  1  0  0  1  0

IIUC get_dummies

pd.get_dummies(df.set_index(['RecordID','Para']),prefix='',prefix_sep = '').sum(level=[0,1]).gt(0).astype(int)
Out[272]: 
               x  y  z  a  b  c
RecordID Para                  
1        A     1  0  1  1  1  1
2        B     0  1  0  1  1  0

Update

pd.get_dummies(df.set_index(['RecordID','Para']),prefix='',prefix_sep = '').sum(level=[0,1]).gt(0).astype(int).replace(0,np.nan).groupby(level=0).ffill().fillna(0)
Out[292]: 
                 x    y    z  a    b    c
RecordID Para                            
1        A     1.0  0.0  1.0  1  1.0  1.0
2        B     0.0  1.0  0.0  1  1.0  0.0
1        C     1.0  0.0  1.0  1  1.0  1.0

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