Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create columns based on rows

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading