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

Replace pandas DataFrame column values with dict values looked up by separate column

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:

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

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
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