How do I deal with a number of different types of valueError results in Python / Pandas? -


i trying extract value input dirty number of possibilities.

the input series has number of possible types example: "8673331000", "8673331000'", 8673331000, 18673331000, 8673331000.0, nan, "867b331000"

in first 5 cases, looking 867333, in int format. last 2 cases report unknown or effect.

i've been using try / except, there number of different types of valueerror reported.

right using:

*try:     val = int(number) except valueerror ve:     if (number[len(number)-1]=="'"):         val = int(number[0:len(number)-2])     else:         val = int(float(number))* 

this except clause handles case input has single quotation mark not handle nan case.

thanks thoughts.

if input pandas series, can use pandas.to_numeric(your_series_data, errors='coerce').fillna(-9999).astype(int)

import pandas pd io import stringio  # example data data=stringio('''values ,8673331000 ,8673331000 ,8673331000 ,18673331000 ,8673331000.0 ,nan ,867b331000 ''')  #read data csv df = pd.read_csv(data, sep=",")  # data may pandas series, below df.iloc[:,0] pd.to_numeric(df.iloc[:,0],errors='coerce').fillna(-9999).astype(int)  nan     8673331000  nan     8673331000 nan     8673331000 nan    18673331000 nan     8673331000 nan          -9999 nan          -9999 name: values, dtype: int64 

you can use values of series directly, without having use try/except coerce them integers.

integers can't represented nan in pandas, in example replaced -9999. when extracting values series, if matches -9999, can set none or whatever value code should have missing value.

if input values strings may coerced nan instead of integers, e.g.

data=stringio('''values ,8673331000 ,"8673331000" ,\'8673331000\' ,18673331000 ,8673331000.0 ,nan ,867b331000 ''') df = pd.read_csv(data, sep=",") 

the 3rd value end nan when using pd.to_numeric(df.iloc[:,0], errors='coerce').fillna(-9999).astype(int) in case suggest removing " or ' input data

hope helps!


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -