2

I have tried to use

if df.loc[df['col_1']] == float:
    print(df.loc[df['col_1']])

But that doesn't work. I basically just want to find everything of the datatype float in a column and see what it is and where. How do I go about doing that?

I need to do this because the column is an object according df.dtypes but in trying to do string operations on it, I am getting a TypeError that there are floats.

DrakeMurdoch
  • 765
  • 11
  • 26
  • 1
    data type in a single column must be homogeneous or a python object. `df.dtype` – Khalil Al Hooti Nov 01 '18 at 17:36
  • 1
    It's weird that you would have data of different types in a single column. In any case, see [Determine the type of an object?](https://stackoverflow.com/questions/2225038/determine-the-type-of-an-object) – lakshayg Nov 01 '18 at 17:37

2 Answers2

5

So I assuming you have column type is object , usually pandas only have one data type per columns

df.col_1.map(type)==float# will return bool
BENY
  • 317,841
  • 20
  • 164
  • 234
3

Use a Boolean mask to perform operations only on strings. This assumes that your series only consist of numeric and string types.

df = pd.DataFrame({'A': [1, 2, 'hello', 'test', 5, 'another']})

num_mask = pd.to_numeric(df['A'], errors='coerce').isnull()

df.loc[num_mask, 'A'] += ' checked!'

print(df)

                  A
0                 1
1                 2
2    hello checked!
3     test checked!
4                 5
5  another checked!
jpp
  • 159,742
  • 34
  • 281
  • 339