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

Pandas – Changing header in a Dataframe using data in another Dataframe

I am trying to rename columns of my Dataframe using data available in another Dataframe. Given below are the dataframes I currently have:

Dataframe 1:

emp_id, Week 41, Week 40, Week 39, Week 38
101, 3, 5, 5, 7 
102, 2, 6, 4, 3
103, 7, 5, 3, 6

Dataframe 2:

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

week, date
Week 35, Aug-29 - Sep-03
Week 36, Sep-05 - Sep-10
Week 37, Sep-12 - Sep-17
Week 38, Sep-19 - Sep-24
Week 39, Sep-26 - Oct-01
Week 40, Oct-03 - Oct-08
Week 41, Oct-10 - Oct-15

Mapping of these Dataframe is using the Week number to date and use that in the main Dataframe

I would like to get the below as the final output:

emp_id, Oct-10 - Oct-15, Oct-03 - Oct-08, Sep-26 - Oct-01, Sep-19 - Sep-24
101, 3, 5, 5, 7 
102, 2, 6, 4, 3
103, 7, 5, 3, 6

>Solution :

Use DataFrame.rename by dictionary from df2:

df1 = df1.rename(columns=df2.set_index('week')['date'].to_dict())
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