wdkrnls
wdkrnls

Reputation: 4702

changing column types of a pandas data frame -- finding offending rows that prevent casting

My PANDAS data has columns that were read as objects. I want to change these into floats. Following the post linked below (1), I tried:

pdos[cols] = pdos[cols].astype(float)

But PANDAS gives me an error saying that an object can't be recast as float.

ValueError: invalid literal for float(): 17_d

But when I search for 17_d in my data set, it tells me it's not there.

>>> '17_d' in pdos
 False

I can look at the raw data to see what's happening outside of python, but feel if I'm going to take python seriously, I should know how to deal with this sort of issue. Why doesn't this search work? How could I do a search over objects for strings in PANDAS? Any advice?

Pandas: change data type of columns

Upvotes: 1

Views: 293

Answers (1)

acushner
acushner

Reputation: 9946

of course it does, because you're only looking in the column list!

'17_d' in pdos

checks to see if '17_d' is in pdos.columns

so what you want to do is pdos[cols] == '17_d', which will give you a truth table. if you want to find which row it is, you can do (pdos[cols] == '17_d').any(1)

Upvotes: 1

Related Questions