简体   繁体   English

Pandas fillna by each Group

[英]Pandas fillna by mean of each Group

I have a pandas dataframe with several columns.我有一个 pandas dataframe 有几个列。 I'd like to fillna's in select columns with mean of each group.我想用fillna's平均值填充 select 列。

import pandas as pd
import numpy as np

df = pd.DataFrame({
                   'cat': ['A','A','A','B','B','B','C','C'],
                   'v1': [10, 12, np.nan, 10, 14, np.nan, 11, np.nan],
                   'v2': [12, 8, np.nan, np.nan, 6, 12, 10, np.nan]
                 })

I am looking for a solution that's scalable, meaning, I could apply do the operation on several columns.我正在寻找一种可扩展的解决方案,这意味着我可以应用在多个列上执行操作。

np.nan 's will be filled with mean of each group. np.nan的将填充每组的mean

Expected output:预计 output:

cat  v1   v2
 
A    10   12
A    12   8
A    11   10
B    10   9
B    14   6
B    12   12
C    11   10
C    11   10

Other similar questions are limited to a single column, I am looking for a solution that is generalizable and works imputing missing NA s for several columns.其他类似的问题仅限于单个列,我正在寻找一种可推广的解决方案,并且可以为多个列输入缺失的NA

This will replace all of the np.nan's with the mean of the column这将用列的平均值替换所有 np.nan

import pandas as pd
import numpy as np

df = pd.DataFrame({
                   'cat': ['A','A','A','B','B','B','C','C'],
                   'v1': [10, 12, np.nan, 10, 14, np.nan, 11, np.nan],
                   'v2': [12, 8, np.nan, np.nan, 6, 12, 10, np.nan]
                 })

for x in df.columns.drop('cat'):
    mean_of_column = df[x].mean()
    df[x].fillna(mean_of_column, inplace = True)
df

Please note that this will make the column a float since them mean is not a neat int.请注意,这将使该列成为浮点数,因为它们的意思不是整洁的整数。 If you wanted to, however, you could continue to work with it to remove the decimal.但是,如果您愿意,可以继续使用它来删除小数点。

Try this:尝试这个:

df = df.fillna(df.groupby('cat').transform('mean'))

Output: Output:

  cat    v1    v2
0   A  10.0  12.0
1   A  12.0   8.0
2   A  11.0  10.0
3   B  10.0   9.0
4   B  14.0   6.0
5   B  12.0  12.0
6   C  11.0  10.0
7   C  11.0  10.0

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

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