python - 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"
解决方案
嗨,请确保
我希望您在填充 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 替换。
推荐阅读
- firebase - 如何知道一个项目在firebase颤振的数组中重复了多少次?
- javascript - 在简单的for循环javascript中等待
- android - 什么时候需要 Modifier.composed { ... }?
- javascript - 画布绘制多行而不是一个 javascript
- http - 使用 Ktor HTTP 客户端(和 kotlinx.io?)直接将可恢复下载到文件
- javascript - PHP JQuery 通过获取 div Id 显示 div 字段
- c# - 为什么事件触发器在统一 2d 中不起作用?
- firemonkey - C++ Builder:转换和打印位图
- r - 如何使用 table() 的一维输出制作 data.frame?
- powershell - 如何从 Jenkins 构建中关注或选择安装向导屏幕