简体   繁体   English

在 Python 中使用 Replace() 或 fillna() 将 NAN 替换为 Pandas 中列的字典值

[英]Replace NAN with Dictionary Value for a column in Pandas using Replace() or fillna() in Python

I'm new to python and I'm trying to use fillna() functionality and facing some problem.我是 python 的新手,我正在尝试使用 fillna() 功能并遇到一些问题。 I have a DataFrame called Temp_Data_DF which has two columns like below:我有一个名为 Temp_Data_DF 的 DataFrame,它有如下两列:

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

I want to replace all NAN with Dict value and resulted dataframe should be like this:我想用 Dict 值替换所有 NAN,结果数据框应该是这样的:

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

I tried the below code:我尝试了以下代码:

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

But its not replacing the NAN with desired value Any help will be appreciated.但它并没有用所需的价值替换 NAN 任何帮助将不胜感激。

I was refering to below link.我指的是下面的链接。

Link: Pandas dataframe fillna() only some columns in place链接: Pandas dataframe fillna() 只有一些列就位

You can fillna by Series created by dictionary :您可以通过dictionary创建的Seriesfillna

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}

Detail :详情

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

How it working:它是如何工作的:

Idea is create new Series with same size like original Series filled by dictionary, so if use fillna by another Series it working nice.想法是创建与字典填充的原始系列相同大小的新Series ,因此如果使用另一个Series fillna很好。

Another solution: Idea is use NaN != NaN , so if use if-else in Series.apply it replace too:另一个解决方案:想法是使用NaN != NaN ,所以如果在Series.apply使用if-else ,它也会替换:

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}

I had a similar problem but @jezrael's approach was not working for me.我遇到了类似的问题,但@jezrael 的方法对我不起作用。 I managed to get it work by creating a series from a list of the default dict.我设法通过从默认字典列表创建一个系列来让它工作。

Temp_Data_DF['B'] = Temp_Data_DF['B'].fillna(pd.Series([{'KEY':1,'VALUE':2}] * Temp_Data_DF.shape[0]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM