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

Find and copy values between 2 dataframes

I’m having trouble copying some data between 2 dataframes. I have the main_df:

id             date  stuff 
123             NaN      3
456      2022-10-10      6
789      2022-09-15      2
357             NaN      9
159      2022-09-15      3

and second_df:

id   stuff 
321      3
456      6
789      2
351      4

I want to search if an id in second_df is in main_df and copy the date that appear in main_df to second_df. This would be the result:

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

id   stuff         date
321      3          NaN
456      6   2022-10-10
789      2   2022-09-15
351      4          NaN  

I know that with second_df["id"].isin(main_id["id"]) I can get a dataframe/column/Series with boolean results indicating if the id exists, but I don’t know how to copy the date value.

Hope someone can help me, thanks.

>Solution :

you can use map to bring over the date value

df2['date']=df2['id'].map(df.set_index('id')['date'])
df2
id  stuff   date
0   321     3   NaN
1   456     6   2022-10-10
2   789     2   2022-09-15
3   351     4   NaN
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