简体   繁体   中英

Pandas column dtype is object but python thinks it is float

I read in a csv like this

df = pd.read_csv(self.file_path, dtype=str)

then I try this:

 df = df[df["MY_COLUMN"].apply(lambda x: x.isnumeric())]

I get an AttributeError:

AttributeError: 'float' object has no attribute 'isnumeric'

Why is this happening? The column contains mostly digits.

I want to filter out the ones where there are no digits.

This question is not how to achieve that or do it better but why do I get an AttributeError here?

Why is this happening?

I think because NaN is not converting to string if use dtype=str , still is missing value, so type=float

Use Series.str.isnumeric for working isnumeric with missing values like all text functions in pandas:

df[df["MY_COLUMN"].str.isnumeric()]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM