1

I have a Panda's DataFrame and in an specific column, there are some values of different type than the desired one. I've tried to use a similar approach like the one exposed here, but it doesn't work. In my case some rows store floats and others store strings, I want to keep only the strings.

This is what I tried.

import pandas as pd

d = {"name": [1.0, 2.0, "hello", "world"]}
df = pd.DataFrame(d)

print df
print df[(type(df["name"]) == type("str"))]

Obviously, I got an error related to a KeyError.

Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93

1 Answers1

1

use the .str accessor with isalpha
this returns NaN for floats so I fillna(False)

df[df.name.str.isalpha().fillna(False)]

enter image description here

piRSquared
  • 285,575
  • 57
  • 475
  • 624