Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: fill in NaN values with dictionary references another column

Tags:

pandas

I have a dictionary that looks like this

dict = {'b' : '5', 'c' : '4'}

My dataframe looks something like this

   A  B
0  a  2
1  b  NaN
2  c  NaN

Is there a way to fill in the NaN values using the dictionary mapping from columns A to B while keeping the rest of the column values?

like image 748
Natasha Avatar asked Mar 17 '17 03:03

Natasha


People also ask

How do I fill NaN based on another column?

Using fillna() to fill values from another column Here, we apply the fillna() function on “Col1” of the dataframe df and pass the series df['Col2'] as an argument. The above code fills the missing values in “Col1” with the corresponding values (based on the index) from “Col2”.

How do I fill NA values in pandas for multiple columns?

We can use fillna() function to impute the missing values of a data frame to every column defined by a dictionary of values. The limitation of this method is that we can only use constant values to be filled.

How do you fill values in a column in a data frame?

ffill() function is used to fill the missing value in the dataframe. 'ffill' stands for 'forward fill' and will propagate last valid observation forward. inplace : If True, fill in place. Note: this will modify any other views on this object, (e.g. a no-copy slice for a column in a DataFrame).


2 Answers

You can map dict values inside fillna

df.B = df.B.fillna(df.A.map(dict))

print(df)

    A   B
0   a   2
1   b   5
2   c   4
like image 198
Vaishali Avatar answered Sep 28 '22 04:09

Vaishali


This can be done simply

df['B'] = df['B'].fillna(df['A'].apply(lambda x: dict.get(x)))

This can work effectively for a bigger dataset as well.

like image 32
abburi Avatar answered Sep 28 '22 05:09

abburi