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.