juypter dataframe去掉Nan

djhell 2021-02-19 02:14:04
juypter

weekly_return 是list

要求必须用到lambda和map
代码如下
import numpy as np
weekly_return = list(weekly_return)
weekly_return_without_NaN = list(map(lambda x:weekly_return.remove(x) if True in np.isnan(x) else x, weekly_return[:]))

出现以下错误
---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-95-2d30654257b9> in <module>
12 from scipy.stats import normaltest
13 weekly_return = list(weekly_return)
---> 14 weekly_return_without_NaN = list(map(lambda x:weekly_return.remove(x) if True in np.isnan(x) else x, weekly_return[:]))
15
16 print("ttt1")

<ipython-input-95-2d30654257b9> in <lambda>(x)
12 from scipy.stats import normaltest
13 weekly_return = list(weekly_return)
---> 14 weekly_return_without_NaN = list(map(lambda x:weekly_return.remove(x) if True in np.isnan(x) else x, weekly_return[:]))
15
16 print("ttt1")

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

...全文
235 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
djhell 2021-02-22
  • 打赏
  • 举报
回复
要求必须用到lambda和map
爱晚乏客游 2021-02-20
  • 打赏
  • 举报
回复
建议使用pandas模块的dropna()处理。

DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)

#axis=0: 删除包含缺失值(NaN)的行

#axis=1: 删除包含缺失值(NaN)的列

# how=‘any’ :要有缺失值(NaN)出现删除

# how=‘all’: 所有的值都缺失(NaN)才删除
# 这两个要配合使用才好。

#还有一个thresh参数,如果缺失值(NaN)的数量大于thresh,将删除

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧