0

I have this dataframe

time id type value
9:04 1  A    23
9:04 1  B    12
9:10 2  A    81
9:10 2  B    17
9:12 3  A    11
9:12 3  B    2
9:14 4  A    88
9:14 4  B    17

I can use this code to turn it into this dataframe

dataframe = dataframe.pivot(index = ['time', 'id'], columns = 'type', values = value)
dataframe.columns = ['_'.join(col) for col in dataframe.columns]

time id A_type B_type
9:04 1  23     NaN
9:04 1  NaN    12
9:10 2  81     NaN
9:10 2  NaN    17
9:12 3  11     NaN
9:12 3  NaN    2
9:14 4  88     NaN
9:14 4  NaN    17

How do I turn this into this dataframe?

time id A_type B_type
9:04 1  23     12
9:10 2  81     17
9:12 3  11     2
9:14 4  88     17
Cauder
  • 2,157
  • 4
  • 30
  • 69

0 Answers0