Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping and pivoting dataframe

Tags:

python

pandas

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?

like image 261
user2896120 Avatar asked Mar 28 '26 19:03

user2896120


1 Answers

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.

like image 185
Corralien Avatar answered Mar 30 '26 12:03

Corralien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!