Home > Software engineering >  Create columns based on rows
Create columns based on rows

Time:09-15

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.

CodePudding user response:

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

CodePudding user response:

What you are looking for are pivot tables

import pandas as pd
from datetime import date

df = pd.DataFrame({
    "Date": [date(2022,1,1), date(2022,1,1), date(2022,1,1), date(2022,1,2), date(2022,1,2), date(2022,1,2)],
    "Name": ["A", "B", "C", "A", "B", "C"],
    "Value": [0, 1, 1, 1, 1, 0]
})

print(pd.pivot_table(df, values="Value", index="Name", columns="Date"))
  • Related