0

At the moment I am processing data obtained from a simulator. This is put into a DataFrame from Pandas. Here the output is:

 1. DateTime, Sensor, Value
 2. 1/13/2021 12:56:10, XTDSX5, 16.55
 3. 1/13/2021 12:56:10, XTDSX6, 55.55
 4. 1/13/2021 12:56:10, XTDSX9p, -15.16
 5. 1/13/2021 12:56:20 PM, XTDSX5, 15.20
 6. 1/13/2021 12:56:20 PM, XTDSX9p, -15.66
 7. 1/13/2021 12:56:30 PM, XTDSX6, 55.56
etc.

Now I want to transform this data to:

 1. DateTime XTDSX5, XTDSX6, XTDSX9p
 2. 1/13/2021 12:56:10, 16.55, 55.55,-15.16
 3. 1/13/2021 12:56:20, 15.20, NaN, -15.66
 4. 1/13/2021 12:56:30, NaN, 55.56, NaN

This to make it fitting for an Artificial Intelligence Classification. Does somebody know how I can do this the best way?

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58

1 Answers1

1
pivoted_df = pd.pivot_table(data=df,
                            index="DateTime",
                            columns="Sensor",
                            )
pivoted_df.reset_index(inplace=True)

Basically, pd.pivot_table will create a new DataFrame, whose index will be the unique elements in DateTime column of original DataFrame df, and whose columns will be unique elements of Sensor column of original df. There is no need to specify value argument of pd.pivot_table, since there is only another column in df. Since a simple index is required by the answer, I then use reset_index, which preserves the DateTime index of pivoted_df as a column.

Enrico Gandini
  • 855
  • 5
  • 29