3

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting this error: all arrays must be same length
Is it possible to create a sample with the error?
Try to do just df.pivot. Is the error still there?
I just changed it to pivot_table and it worked
Strange but ok. I did not manage to reproduce the error. I updated my answer (if you want to accept it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.