简体   繁体   中英

reshape pandas dataframe reversing one row into several columns

I have a dataframe which looks like the below one. It has the ID column, the month, and whether the customer bought or not a specific product.

ID      Date     Buy_Or_Not
1       2016-01       1
1       2016-02       1
1       2016-03       0
1       2016-04       1
1       2016-05       0
2       2016-01       1
2       2016-02       1
2       2016-03       1
2       2016-04       1
2       2016-05       0

I would like to reshape it to look like this.

ID      2016-01 2016-02 2016-03 2016-04 2016-05
1       1       1       0       1       0
2       1       1       1       1       0

Any advice in getting this done.

Here are 3 ways of reshaping

1) Using pd.pivot

In [58]: df.pivot(index='ID', columns='Date', values='Buy_Or_Not')
Out[58]:
Date  2016-01  2016-02  2016-03  2016-04  2016-05
ID
1           1        1        0        1        0
2           1        1        1        1        0

2) Using pd.crosstab

In [59]: pd.crosstab(df['ID'], df['Date'], df['Buy_Or_Not'], aggfunc=sum)
Out[59]:
Date  2016-01  2016-02  2016-03  2016-04  2016-05
ID
1           1        1        0        1        0
2           1        1        1        1        0

3) Using groupby and unstack

In [60]: df.groupby(['ID', 'Date']).sum().unstack('Date')
Out[60]:
     Buy_Or_Not
Date    2016-01 2016-02 2016-03 2016-04 2016-05
ID
1             1       1       0       1       0
2             1       1       1       1       0

Use pivot :

df = df.pivot(index='ID', columns='Date', values='Buy_Or_Not')
print (df)
Date  2016-01  2016-02  2016-03  2016-04  2016-05
ID                                               
1           1        1        0        1        0
2           1        1        1        1        0

If errors like:

ValueError: Index contains duplicate entries, cannot reshape

use pivot_table .

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