I am new to pandas and I am looking for a nice way to transform this dataframe:
| Date | Name | Value |
|---|---|---|
| 01-01-2022 | A | 0 |
| 01-01-2022 | B | 1 |
| 01-01-2022 | C | 1 |
| 02-01-2022 | A | 1 |
| 02-01-2022 | B | 1 |
| 02-01-2022 | C | 0 |
To this dataframe:
| Name | Value_before | Value_after |
|---|---|---|
| A | 0 | 1 |
| B | 1 | 1 |
| C | 1 | 0 |
First table contains only data from two dates.
>Solution :
Assuming:
- that you have only 2 dates
- that there are no duplicated Name per date
You can use a pivot taking advantage of the fact the pivot sorts the columns, then set_axis to use your custom names
out = (df
.assign(Date=pd.to_datetime(df['Date'])) # ensure datetime for correct sorting
.pivot('Name', 'Date', 'Value')
.set_axis(['Value_before', 'Value_after'], axis=1)
.reset_index()
)
output:
Name Value_before Value_after
0 A 0 1
1 B 1 1
2 C 1 0