Reputation: 45
I know this question has been asked in multiple threads in stackoverflow, but I haven't able to achieve how I wanted data to be transposed. I am complete beginner at python mostly work with sql.
I have values saved in dataframe in format like mentioned below
order_id primary_dish primary_cat dish_id
912574 54465 2423 54481
912574 54465 2423 54540
912574 54481 2425 54465
912574 54481 2425 54540
912574 54540 2429 54481
912574 54540 2429 54465
I want this data to be represented as
order_id primary_dish primary_cat 54481 5450 54465
912574 54465 2423 1 1 0
912574 54481 2425 0 1 1
912574 54540 2429 1 0 1
Basically the last column dish_id
in the saved data frame df is
transposed and the values present for that primary dish are represented by 1
and if not present are represented by 0
Upvotes: 3
Views: 1015
Reputation: 323226
More easy way. using pd.crosstab
pd.crosstab(df['dish_id'],[df['order_id'],df['primary_dish'],df['primary_cat']]).T.reset_index()
Upvotes: 1
Reputation: 210822
Try this:
In [5]: df.pivot_table(index=['order_id','primary_dish','primary_cat'],
columns='dish_id', aggfunc='size', fill_value=0) \
.reset_index()
Out[5]:
dish_id order_id primary_dish primary_cat 54465 54481 54540
0 912574 54465 2423 0 1 1
1 912574 54481 2425 1 0 1
2 912574 54540 2429 1 1 0
Upvotes: 4