I need to convert data in a pandas dataframe in a single column into a dataframe with multiple columns.
Original Dataframe Data:
Date | Type | Value |
---|---|---|
10/14/2022 12:35:00 PM | TypeA | 1 |
10/14/2022 12:35:00 PM | TypeB | 2 |
10/14/2022 12:35:00 PM | TypeC | 4 |
10/15/2022 12:35:00 PM | TypeA | 6 |
10/15/2022 12:35:00 PM | TypeB | 17 |
10/15/2022 12:35:00 PM | TypeC | 4 |
10/16/2022 12:35:00 PM | TypeA | 3 |
10/16/2022 12:35:00 PM | TypeB | 1 |
10/16/2022 12:35:00 PM | TypeC | 12 |
Here is what the data needs to look like when rearranged:
Date | TypeA | TypeB | TypeC |
---|---|---|---|
10/14/2022 12:35:00 PM | 1 | 2 | 4 |
10/15/2022 12:35:00 PM | 6 | 17 | 4 |
10/16/2022 12:35:00 PM | 3 | 1 | 12 |
Here is the code to create the original dataframe:
import pandas as pd
data = [['10/14/2022 12:35:00 PM', 'TypeA', 1],
['10/14/2022 12:35:00 PM', 'TypeB', 2],
['10/14/2022 12:35:00 PM', 'TypeC', 4],
['10/15/2022 12:35:00 PM', 'TypeA', 6],
['10/15/2022 12:35:00 PM', 'TypeB', 17],
['10/15/2022 12:35:00 PM', 'TypeC', 4],
['10/16/2022 12:35:00 PM', 'TypeA', 4],
['10/16/2022 12:35:00 PM', 'TypeB', 1],
['10/16/2022 12:35:00 PM', 'TypeC', 12]]
df = pd.DataFrame(data, columns=['DateTime', 'Type', 'Value'])
How do I programmatically convert df to the desired arrangement?