Hisham
Hisham

Reputation: 253

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

Upvotes: 3

Views: 58

Answers (2)

Ukrainian-serge
Ukrainian-serge

Reputation: 854

Regular pivot works as well:

df.pivot(values='dcount', index='entity_name', columns='request_status').fillna(0).astype(int)

Upvotes: 3

warped
warped

Reputation: 9481

you can use pivot_table:

a = pd.pivot_table(df, values = 'dcount', index='entity_name', columns='request_status').fillna(0)
a = a.astype(int)

Upvotes: 2

Related Questions