简体   繁体   English

根据“类型”列为分类数据估算 Nan

[英]Imputate Nan for categorical data depending on its "Type" column

I have dataframe with 2 columns Name and Signal.我有 dataframe 有 2 列名称和信号。 I want to fill nan values in Signal column but it should be done according to its Name.我想在 Signal 列中填写 nan 值,但应该根据其名称来完成。 I want to imputate it with Most frequent value according to its Name.我想根据它的名称用最常见的值来估算它。 For example:例如:

Timestamp   Name  Signal
 2021-01-01  A.     On
 2021-01-02. A      nan
 2021-01-03. A.     On 
 2021-01-01. B.     Off
 2021-01-02. B.     Off
 2021-01-03. B.     nan

For name A nan value of Signal column should be imputated with "On" since it is most frequent value but for Name B it should be filled with Off because it is the most frequent for B.对于名称 A 的 Signal 列的 nan 值应该用“On”进行估算,因为它是最常见的值,但对于名称 B,它应该填充为 Off,因为它是 B 最常见的值。

How can I achieve it?我怎样才能实现它?

df = df.groupby('Name').apply(lambda x: x.fillna(x['Signal'].value_counts().index[0]))

Output: Output:

>>> df
    Timestamp Name Signal
0  2021-01-01    A     On
1  2021-01-02    A     On
2  2021-01-03    A     On
3  2021-01-01    B    Off
4  2021-01-02    B    Off
5  2021-01-03    B    Off

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

相关问题 Matplotlib 和 Cartopy - 如何根据分类列自动使用 colors 自动 plot 数据? - Matplotlib & Cartopy - How to automatically plot data with colors depending on a categorical column? 从分类数据类型列中提取均值 - Extract mean from categorical data type column Pandas:根据产品类型将零值和 NAN 值替换为平均值列 - Pandas: Replace zero and NAN values with column Mean depending on product type 熊猫根据类型替换nan - Pandas replace nan depending on type 为什么一列分类数据未正确绘制其关联值? - Why is a column of categorical data not correctly plotted with its associated values? 如何随机填充分类数据的 NaN? - How to fill NaN for categorical data randomly? 如何将列数据类型从“字符串”转换为“布尔”,保留 NaN? - How to convert a column data type from 'string ' to 'boolean', preserving NaN? 根据列的原始数据类型更改 Pandas Dataframe 中列的数据类型 - Change datatype of columns in Pandas Dataframe depending on the original data type of the column 根据其左侧单元格的值在 Pandas DataFrame 中填充 NaN 值 - Fill NaN values in a pandas DataFrame depending on values of cells to its left 将分类列添加到数据框并将类别与现有的分类列匹配 - Add categorical column to data frame and match categories with existing categorical column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM