8-Bit Borges
8-Bit Borges

Reputation: 10033

Pandas - assign column values to new columns names

I have this dataframe:

player_id    scout_occ  round scout
812842         2        1     X
812842         4        1     Y
812842         1        1     Z
812842         1        2     X
812842         2        2     Y
812842         2        2     Z

And I need to transpose 'scout' values to columns, as well as using number of occurrences as value or these new columns, ending up with:

player_id     round  X  Y  Z
812842        1      2  4  1
812842        2      1  2  2

How do I achieve this?

Upvotes: 1

Views: 61

Answers (1)

hasanyaman
hasanyaman

Reputation: 328

Use pivot_table. For example:

df = df.pivot_table(values='scout_occ',index=['player_id','round'],columns='scout')

Then if you don't want to use column name(scout):

df.columns.name = None

Also, if you want to use player_id and round as a column not as an index:

df.reset_index()

Upvotes: 2

Related Questions