0

I have a dataframe, which has a column representing some classes or types, e.g.

|datetimeindex   |  class |
+----------------+--------+
|2021-11-11 09:00|    1   |
|2021-11-11 10:00|   -1   |
|2021-11-12 11:00|    2   |
|2021-11-14 09:00|    2   |
|2021-11-15 09:30|    5   |
|2021-11-16 09:30|    3   |
|2021-11-17 09:30|    9   |
|2021-11-18 09:30|   -5   |

class column is of integers, with each integer represent a kind of class.

e.g. Line 1 has a value of 1 for class column, which means this row belongs to CLASS1.

what I want is an efficient pandas method to create multiple columns, and each column means an indicator variable that represents if this row belongs to the column class.

To make it more clear, an expected output for the sample dataframe would look like below.

|datetimeindex   |class | CLASS-5 |  CLASS-1 |  CLASS1 |  CLASS2 |  class3 |...|
+----------------+------+---------+----------+---------+---------+---------+
|2021-11-11 09:00|  1   |    0    |    0     |    1    |    0    |    0    |
|2021-11-11 10:00| -1   |    0    |    1     |    0    |    0    |    0    |
|2021-11-12 11:00|  2   |    0    |    0     |    0    |    1    |    0    |
|2021-11-14 09:00|  2   |    0    |    0     |    0    |    1    |    0    |
|2021-11-15 09:30|  5   |    0    |    0     |    0    |    0    |    0    |
|2021-11-16 09:30|  3   |    0    |    0     |    0    |    0    |    1    |
|2021-11-17 09:30|  9   |    0    |    0     |    0    |    0    |    0    |
|2021-11-18 09:30| -5   |    1    |    0     |    0    |    0    |    0    |
Vy Do
  • 46,709
  • 59
  • 215
  • 313
Dai Zhang
  • 71
  • 7

0 Answers0