0

I need to change the structure of the python DataFrame from "two-rows with flag" into "onew row, two columns" like that:

from

    ID  Name Hours     LogFlag
1:  1  One    1       log
1:  1  One    10      non-log
3:  2  Two    5       log
4:  2  Two    15      non-log

into

    ID  Name LogHours NonLogHours
1:  1   One    1        10
2:  2   Two    5        15

I'm trying to come up with .apply() lambda function to make the process more effective, but not sure how to implement it here. The DataFrame is quite big, so I want to make it time- and memory-effective, as far as I know, just to iterate trough rows of DF with "if" would be too much time-consuming. Maybe it can be achieved with something using "loc/iloc" - any ideas?

Would gladly appreciate any help, thank you in regards!

  • 3
    `df.pivot(index=['ID', 'Name'], columns='LogFlag', values='Hours').add_suffix('Hours').reset_index()` (you might first need to rework slightly the strings in LogFlag) – mozway May 18 '22 at 12:41

0 Answers0