Skip to content
Advertisement

How to change index and transposing in pandas

I’m new in pandas and trying to do some converting on the dateframe but I reach closed path.

my data-frame is:

  entity_name  request_status  dcount
0  entity1          0           1
1  entity1          1           6
2  entity1          2          13
3  entity2          1           4
4  entity2          2           7

I need this dataframe to be like the following:

index      0      1      2
entity1    1      6      13
entity2    0      4      7

as it shown I take the entity_name column as index without duplicates and the columns names from request_status column and the value from dcount

so please any one can help me to do that ?

many thanks

Advertisement

Answer

you can use pivot_table:

a = pd.pivot_table(df, values = 'dcount', index='entity_name', columns='request_status').fillna(0)
a = a.astype(int)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement