0

I want to update empty rows in dataframe1 with the equivalent values from dataframe2 only if the rows in dataframe1 is empty.

Cases:

Dataframe1

fig 1

Dataframe2

fig 2

In the above example, I want to fill only empty rows of Price columns in dataframe1 with the equivalent Price columns from dataframe2.

Any ideas or suggestions for this?

import pandas as pd

df1 = pd.read_csv('dataframe1.csv')
df2 = pd.read_csv('dataframe2.csv')

1 Answer 1

2

Try this:

 df2dict = df2.set_index(['Product Code'])['Price'].squeeze().to_dict()
 # maps from df2['Product Code'] to empty columns in df
 df['Price'] = df['Price'].fillna(df['Product Code'].map(df2dict))
Sign up to request clarification or add additional context in comments.

1 Comment

I see that this post was edited, but shouldnt it be df['Product Code'].map(df2dict)? This will return a series that matches df1, but will only fill in the nan values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.