Python transpose one row into a column

I know this question has been asked on multiple threads on stackoverflow, but I haven't been able to achieve how I wanted the data to be transposed. I am a complete newbie to python, mainly working with sql.

I have values ​​stored in dataframe in format as 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 presented 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 main cymbal are represented by 1 and if not, then they are represented by 0

+3


source to share


2 answers


Try the following:



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

      

+4


source


An easier way. usingpd.crosstab



 pd.crosstab(df['dish_id'],[df['order_id'],df['primary_dish'],df['primary_cat']]).T.reset_index()

      

+1


source







All Articles