pandas 菲娜模式 [英] Pandas Fillna Mode
本文介绍了 pandas 菲娜模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据集,其中有一个称为本国"的列,其中包含30000
条记录.有些缺少以NaN
表示的内容,所以我想用mode()
值填充它.我写了这样的东西:
I have a data set in which there is a column known as Native Country which contain around 30000
records. Some are missing represented by NaN
so I thought to fill it with mode()
value. I wrote something like this:
data['Native Country'].fillna(data['Native Country'].mode(), inplace=True)
但是,当我计算缺失值时:
However when I do a count of missing values:
for col_name in data.columns:
print ("column:",col_name,".Missing:",sum(data[col_name].isnull()))
对于本国"列,它仍会提供相同数量的NaN
值.
It is still coming up with the same number of NaN
values for the column Native Country.
推荐答案
只需调用系列的第一个元素:
Just call first element of series:
data['Native Country'].fillna(data['Native Country'].mode()[0], inplace=True)
或者您也可以使用asssgnment做同样的事情:
or you can do the same with assisgnment:
data['Native Country'] = data['Native Country'].fillna(data['Native Country'].mode()[0])
这篇关于 pandas 菲娜模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文