0

I have two pandas dataframes, and both dataframes have equal number of rows and columns.

Only the first column in both dataframes have non NaN values. For other columns, it might be either NaN or some integer.

I need to compare the two dataframes using the first column, and update the value of the other columns in the second dataframe to NaN, if it is NaN in the first dataframe, irrespective of any value in the second dataframe.

I tried bruteforce method by converting df into dictionary, and compare each values. Is there a simple way to achieve this using Pandas functions?

ahairshi
  • 381
  • 3
  • 18
  • couple ways to do it in pandas, couple ways in numpy. this thread goes over those ways and the time they take. https://stackoverflow.com/questions/39903090/efficiently-replace-values-from-a-column-to-another-column-pandas-dataframe – smcrowley Jul 12 '22 at 17:59
  • Please have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and [edit] your question to include samples of your inputs and your expected output so that we can better understand how to help – G. Anderson Jul 12 '22 at 18:00
  • `df2.set_index('col1').where(df1.set_index('col1').isna())` – Quang Hoang Jul 12 '22 at 18:12

0 Answers0