【发布时间】:2018-03-07 12:48:35
【问题描述】:
我有两种类型的错误数据需要更正。一个是null,一个是nan。
>>> df_new
Volume Price
Date
2017-01-01 500 760
2017-01-02 null 760
2017-01-03 50 770
2017-01-04 null 780
另一种类型是NaN
>>> df_new
Volume Price
Date
2017-01-01 500 760
2017-01-02 NaN 760
2017-01-03 50 770
2017-01-04 NaN 780
如何将 null 和 NaN 数据都替换为 0? 如果为 null 或 NaN,我的代码工作,但我不能同时工作
volume = df_new['Volume'] == 'null' or df_new['Volume'].isnull()
df_new.loc[volume,'Volume'] = 0
df_new.replace('null',np.NaN,inplace=True)
df_new.iloc[0].fillna(df_new.iloc[1].Open,inplace=True)
返回错误
Traceback(最近一次调用最后一次):文件“”,第 1 行,in 文件 “/home/.local/lib/python2.7/site-packages/pandas/core/ops.py”,行 763,在包装器 res = na_op(values, other) 文件中 “/home/.local/lib/python2.7/site-packages/pandas/core/ops.py”,行 718、在 na_op 中引发 TypeError("invalid type comparison")TypeError: 无效的类型比较
如果volume = df_new['Volume'] == 'null' 代码将起作用,但如果它是 NaN,这将无法纠正数据,并用 0 替换
【问题讨论】: