简体   繁体   English

如何在 Python 中的数据框中填充类型为 Float 的列的模式值

[英]How to fillna with mode value of a Column with type Float in a data frame in Python

当我尝试用mode填充NaN值时,我发现它再次返回Nan值。

df= df.apply(lambda x: x.fillna(x.mode() if (x.dtypes!=object ) else x.value_counts().index[0] ))

Use x.mode().item()使用x.mode().item()

like in your example:就像你的例子:

>>> df.apply(lambda x: x.fillna(x.mode().item() if (x.dtypes!=object ) else x.va
lue_counts().index[0] ))

Note: It only works when the mode is single or scalar value.注意:仅在模式为或标量值时有效。 - as remarked by @jezrael - 正如@jezrael 所说

暂无
暂无

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

相关问题 如何在Python的数据框中使用浮点数据替换列中的Nan - How to replace Nan from column with float data in a data frame in python 逐列填充模式 - Fillna with mode column by column 使用模式将 NaN 替换为最常见的列字符串值时,我无法让 Python 中的 Fillna 工作 - I cannot get Fillna in Python to Work when using Mode to Replace NaNs with Most Frequent Column String Value Python Dataframe fillna 左列有值 - Python Dataframe fillna with value on left column 如何使用零值进行填充,直到数据出现在每列中,然后对 pandas 数据帧中的每一列使用前向填充 - How to do a fillna with zero values until data appears in each column, then use the forward fill for each column in pandas data frame 如何从 pandas 数据帧中的时间戳列中提取小时数并转换为浮点类型 - How to extract hours from time stamp column in pandas data frame and convert to float type 数据框列不会转换为 python 中的浮点数据类型 - Data frame columns wont convert to float data type in python 使用fillna(method ='bfill')和group by的Python3熊猫数据框 - Python3 pandas data frame using fillna(method='bfill') with group by 在数据框上按行将浮点类型数转换为百分比 - Python - Turn a float type number into percentage by row on a Data Frame- Python 如何将值列表中的数据框转换为大数据框,其中每个级别在python中为单列值? - how to convert a data frame with a list in the value to a big data frame with the each level as a single column value in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM