dartdog dartdog - 1 year ago 120
Python Question

Data type conversion issue in Pandas Dataframe

I have a csv file that looks like this from a !cat


When I read it in via a read_csv I get a dataframe all of the ..._exp fields are single digit numbers that I need to do basic math with (It was working great when I was using read-table with another variant of the file)

df = pd.io.parsers.read_csv('/home/tom/Dropbox/Projects/annonallanswerswithmaster1012013.csv',index_col=0,parse_dates=['Quradate'])

But when I go to do any math I get a type error indicating the column is string eg:

df['Credit_exp'] = df['Credit_exp']/2
TypeError: unsupported operand type(s) for /: 'str' and 'int'

I don't see how to convert or get it as a int?
I tried specifying field types like ,dtype={'Credit_exp': np.int32, ... in the file read options,, it did not like that
and I tried to do a type conversion like
df['Credit_exp'] = int(df['Credit_exp'])
Which just gave me:

TypeError: only length-1 arrays can be converted to Python scalars

So there is something obvious I'm missing...

Answer Source
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download