Create columns based on rows

Question:

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.

Asked By: kstajer

||

Answers:

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
Answered By: mozway

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"))
Answered By: Matteo Zanoni
def function1(dd:pd.DataFrame):
    dd1=dd.loc[:,'Value']
    dd1.index=['Value_before','Value_after']
    return dd1
df1.groupby("Name").apply(function1)

Value  Value_before  Value_after
Name                            
A                 0            1
B                 1            1
C                 1            0
Answered By: G.G
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.