1

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

Community
  • 1
  • 1
wdkrnls
  • 4,548
  • 7
  • 36
  • 64

1 Answers1

1

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)

acushner
  • 9,595
  • 1
  • 34
  • 34