2

I want to drop all rows of my dataframe where the value in a row is not str

# example data
df = pd.DataFrame([(1, 'test1'), (2, 'test2'), (3, 3.14)])
df.columns = ['row1', 'row2']

I came up with this solution, but is there a simpler, more pythonic way? Having to use apply() seems messy.

 df[df['row2'].apply(type) == str]
max
  • 4,141
  • 5
  • 26
  • 55
  • For rows that might contain the string representation of a number, maybe try `[isinstance(i, str) for i in df.row2]`. Although now that's compromising efficiency, but it will beat `apply` – user3483203 Sep 09 '18 at 19:58
  • You can use isnumeric. `df[~df['row2'].str.isnumeric().fillna(True)]` – ayhan Sep 09 '18 at 19:58

0 Answers0