2017-09-26 47 views
0

我有两种错误的数据需要修正。一个是无效的,一个是与南。的Python:熊猫会导致无效类型比较

>>> 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 

另一种类型是与南

>>> 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 

如何用0代替null和NaN的数据? 我的代码工作,如果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 (most recent call last): File "", line 1, in File "/home/.local/lib/python2.7/site-packages/pandas/core/ops.py", line 763, in wrapper res = na_op(values, other) File "/home/.local/lib/python2.7/site-packages/pandas/core/ops.py", line 718, in na_op raise TypeError("invalid type comparison")TypeError: invalid type comparison

代码将工作,如果volume = df_new['Volume'] == 'null'但这不会更正数据是很NaN和0

回答

0

使用replace repalce用于替换nullfillna用于替换NaN S和None S:

df['Volume'] = df['Volume'].replace('null', np.nan).fillna(0) 

或者:

对于检测nullNaN的add |按位or和括号:

volume = (df_new['Volume'] == 'null') | (df_new['Volume'].isnull()) 
+0

对不起,它需要检测大熊猫据帧包含NaN或空第一,那是对的吗?该probllem是在'体积= df_new [ '音量'] == '空' 或df_new [ '体积']。ISNULL()' – lotteryman

+0

这时需要'体积=(df_new [ '音量'] == '空') | (df_new ['Volume']。isnull())' – jezrael

+0

'volume =(df_new ['Volume'] =='null')| (df_new [ '体积'] ISNULL())'仍然返回错误'类型错误:无效类型comparison' – lotteryman