Use Replace() or fillna() in Python to replace NAN with dictionary value of column in Pandas


sindu

I'm new to python and trying to use the fillna() function and having some problems. I have a DataFrame called Temp_Data_DF which has two columns like this:

Temp_Data_DF:
A  B
1  NAN
2  NAN
3  {'KEY':1,'VALUE':2}

I want to replace all NANs with Dict values, the resulting dataframe should look like this:

Temp_Data_DF:
A  B
1  {'KEY':1,'VALUE':2}
2  {'KEY':1,'VALUE':2}
3  {'KEY':1,'VALUE':2}

I tried the following code:

Bvalue = {'KEY':1,'VALUE':2}
Temp_Data_DF['B']=Temp_Data_DF['B'].fillna(Bvalue)

But it cannot replace NAN with the expected value. I will really appreciate.

I am referring to the link below.

Link: Pandas dataframe fillna() exists only for some columns

Jesler

You can do fillnathis by Seriescreating dictionary:

Bvalue = {'KEY':10,'VALUE':20}
Temp_Data_DF['B']=Temp_Data_DF['B'].fillna(pd.Series([Bvalue], index=Temp_Data_DF.index))
print (Temp_Data_DF)
   A                         B
0  1  {'VALUE': 20, 'KEY': 10}
1  2  {'VALUE': 20, 'KEY': 10}
2  3    {'VALUE': 2, 'KEY': 1}

Details :

print (pd.Series([Bvalue], index=Temp_Data_DF.index))
0    {'VALUE': 20, 'KEY': 10}
1    {'VALUE': 20, 'KEY': 10}
2    {'VALUE': 20, 'KEY': 10}
dtype: object

Mode of operation:

The idea is to create the new one with Seriesthe same size as the original series filled by the dictionary , so if used fillnaby another , Seriesit works great.

Another solution: the idea is to use NaN != NaN, so also replace if used if-elsein there:Series.apply

Bvalue = {'KEY':10,'VALUE':20}
Temp_Data_DF['B']=Temp_Data_DF['B'].apply(lambda x: x if x == x else Bvalue)
print (Temp_Data_DF)
   A                         B
0  1  {'KEY': 10, 'VALUE': 20}
1  2  {'KEY': 10, 'VALUE': 20}
2  3  {'KEY': 10, 'VALUE': 20}

Related


Python Pandas replace value in column

Gianjie When I look at the values in a column in the dataframe, I can see that the same category is mistyped due to user data entry errors. For my dataframe I use the following code: df['column_name'].value_counts() output: Targeted 523534 targeted 1

pandas python replace column value

Broadcast time Suppose I have a dataframe with columns a: 1, 2, 3, 4, 5, .... How can I replace these values so that each number is 1 less (the new a is 0, 1, 2, 3, 4). thank you all. Andy Hayden just += 1: In [11]: df = pd.DataFrame({'a': [1, 2, 3, 4, 5]}) I

Python Pandas replace value in column

Gianjie When I look at the values in a column in the dataframe, I can see that the same category is mistyped due to user data entry errors. For my dataframe I use the following code: df['column_name'].value_counts() output: Targeted 523534 targeted 1

Python Pandas replace value in column

Gianjie When I look at the values in a column in the dataframe, I can see that the same category is mistyped due to user data entry errors. For my dataframe I use the following code: df['column_name'].value_counts() output: Targeted 523534 targeted 1

Python Pandas replace value in column

Gianjie When I look at the values in a column in the dataframe, I can see that the same category is mistyped due to user data entry errors. For my dataframe I use the following code: df['column_name'].value_counts() output: Targeted 523534 targeted 1