简体   繁体   中英

Python: Pandas cause invalid type of comparison

I have two types of error data that need to corrected. One is with null and one is with 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

Another type is with 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

How to replace both null and NaN data with 0? My code working if either null or NaN but I can't work for both

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)

it return error

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

The code will work if volume = df_new['Volume'] == 'null' but this will not correct the data is it is NaN, and repalce with 0

Use replace for replace null and fillna for replace NaN s and None s:

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

Or:

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

For detect null or NaN s add | for bitwise or and parentheses:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM