首页 > 解决方案 > TypeError:“value”参数必须是标量、dict 或 Series,但您在 Python 中传递了“DataFrame”

问题描述

目前我正在讨论主题问题。我不确定它为什么会破坏以及需要进行哪些修改。

代码:

table = df.pivot_table(values='LoanAmount', index='Self_Employed' ,columns='Education', aggfunc=np.median)

def fage(x):
    return table.loc[x['Self_Employed'],x['Education']]

#Replacing missing values

df['LoanAmount'].fillna(df[df['LoanAmount'].isnull()].apply(fage,axis=1),inplace =True) 

输出:

[15]: df['LoanAmount'].fillna(df[df['LoanAmount'].isnull()].apply(fage,axis=1),inplace =True)

回溯(最近一次通话最后):

 File "<ipython-input-15-dadf94659135>", line 1, in <module>
    df['LoanAmount'].fillna(df[df['LoanAmount'].isnull()].apply(fage,axis=1),inplace =True)

  File "/home/aryabhatta/anaconda3/lib/python3.6/site-packages/pandas/core/series.py", line 3422, in fillna
    **kwargs)

  File "/home/aryabhatta/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 5400, in fillna
    '"{0}"'.format(type(value).__name__))

TypeError: "value" parameter must be a scalar, dict or Series, but you passed a "DataFrame"

标签: pythonpandas

解决方案


嗨,请确保

我希望您在填充 LoanAmount 的缺失值之前没有执行此行

df['LoanAmount'].fillna(df['LoanAmount'].mean(), inplace=True)

错误是 LoanAmount 已经填充了其他值,并且其中没有缺失值。因此它会引发错误。

替换缺失值:

df['LoanAmount'].fillna(df[df['LoanAmount'].isnull()].apply(fage,axis=1),inplace =True

在执行上述代码之前,请确保LoanAmount在下面的命令中执行缺失值以进行检查。

df.apply(lambda x: sum(x.isnull()),axis=0)

上面的命令将显示LoanAmount. 如果你有缺失值,那么只有你可以用 fage 或 mean 替换。


推荐阅读