I have a dataframe that looks like this:
Names Company Values Period
HeadCount Google 1000 1
HoursWorked Google 500 1
HeadCount Microsoft 600 1
HoursWorked Microsoft 200 1
HeadCount Google 2000 2
HoursWorked Google 100 2
I'd like to transform this dataframe to something like this:
Company Period HeadCount HoursWorked
Google 1 1000 500
Google 2 2000 100
Microsoft 1 600 200
Is there a way to take the columns pivot them, but also group by the company period and have a dataframe that looks like that?
Use pivot_table:
>>> df.pivot_table(index=['Company', 'Period'], columns='Names', values='Values') \
.rename_axis(None, axis=1).reset_index()
Company Period HeadCount HoursWorked
0 Google 1 1000 500
1 Google 2 2000 100
2 Microsoft 1 600 200
Edit:
I'm getting this error: all arrays must be same length
Solution: replace pivot by pivot_table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With