Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用pandas中的平均值和模式替换缺少的值_Python_Pandas - Fatal编程技术网

Python 用pandas中的平均值和模式替换缺少的值

Python 用pandas中的平均值和模式替换缺少的值,python,pandas,Python,Pandas,我在熊猫中有以下数据帧 C1 C2 C3 10 a b 10 a b ? c c ? ? b 10 a b 10 ? ? 如果适用,我想用平均值和模式替换? 我想要的数据帧是 C1 C2 C3 10 a b 10 a b 10

我在熊猫中有以下数据帧

 C1      C2       C3
 10      a        b
 10      a        b
 ?       c        c
 ?       ?        b
 10      a        b
 10      ?        ?
如果适用,我想用平均值和模式替换

我想要的数据帧是

 C1      C2       C3
 10      a        b
 10      a        b
 10      c        c
 10      a        b
 10      a        b
 10      a        b 
我试过跟随熊猫,但没有成功

 df[df['C1'] == '?'] = df[df['C1' != '?'].mean()

您需要分别处理数字列和非数字列

df.replace('?', np.nan, inplace = True)

df['C1'] = df['C1'].astype(float)
df['C1'] = df['C1'].astype(float).fillna(df['C1'].mean())

non_numeric = ['C2', 'C3']
df[non_numeric]=df[non_numeric].fillna(df.mode().iloc[0])

    C1      C2  C3
0   10.0    a   b
1   10.0    a   b
2   10.0    c   c
3   10.0    a   b
4   10.0    a   b
5   10.0    a   b

@anky_91
C1
是数值变量。请您在这里解释一下
iloc[0]
的作用好吗?