python - How do I shift my pandas dataframe structure? -
suppose have data this:
item | date | status 12-01 e 12-02 e 12-03 b 12-04 b b 12-01 e b 12-02 b b 12-03 s b 12-04 s c 12-03 e c 12-04 e
now want make data below:
item | 12-01 | 12-02 | 12-03 | 12-04 e e b b b e b s s c e e
so in summary, want make each row item's history. heard called 'pivot', i'm not sure doing "easily" pandas.
can me this?!
you can use pivot
:
df = df.pivot(index='item', columns='date', values='status') print (df) date 12-01 12-02 12-03 12-04 item e e b b b e b s s c none none e e
if duplicates in item
date
use pivot_table
aggregate function ','.join
or sum
:
print (df) item date status 0 12-01 e <-duplicates a, 12-01 1 12-01 c <-duplicates a, 12-01 2 12-02 e 3 12-03 b 4 12-04 b 5 b 12-01 e 6 b 12-02 b 7 b 12-03 s 8 b 12-04 s 9 c 12-03 e 10 c 12-04 e df = df.pivot_table(index='item', columns='date', values='status', aggfunc=','.join) print (df) date 12-01 12-02 12-03 12-04 item e,c e b b b e b s s c none none e e
Comments
Post a Comment