Given a pandas DataFrame containing nan:
import numpy as np
import pandas as pd
bad_df = pd.DataFrame({'foo': [np.nan, 1.0, 2.0], 'bar': ['a', 'b', 'c']})
And also given a dictionary with lookup keys that may exist in the bar column:
replace_values = {'a': 7.0, 'd': 10.0}
How can the nan values in the foo column be replace with values in the replace_values dictionary based on the lookup of bar? The resulting DataFrame would look like:
expected_df = pd.DataFrame({'foo': [7.0, 1.0, 2.0], 'bar': ['a', 'b', 'c']})
The typical fillna and replace methods on DataFrames don’t seem to have this functionality since they will replace all na with the same value as opposed to a lookup value.
Thank you in advance for your consideration and response.
>Solution :
You can use loc with a condition using isna, and assign values using map:
bad_df.loc[bad_df['foo'].isna(), 'foo'] = bad_df['bar'].map(replace_values)
Or fillna with map as an alternative:
bad_df['foo'] = bad_df['foo'].fillna(bad_df['bar'].map(replace_values))
Both will ouput:
foo bar
0 7.0 a
1 1.0 b
2 2.0 c