简体   繁体   English

Pandas 列 dtype 为 object 但 python 认为它是浮动的

[英]Pandas column dtype is object but python thinks it is float

I read in a csv like this我读到这样的 csv

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' AttributeError: 'float' object 没有属性 '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?这个问题不是如何实现或做得更好,而是为什么我在这里得到一个AttributeError

Why is this happening?为什么会这样?

I think because NaN is not converting to string if use dtype=str , still is missing value, so type=float我认为因为如果使用dtype=str , NaN 不会转换为string ,仍然缺少值,所以type=float

Use Series.str.isnumeric for working isnumeric with missing values like all text functions in pandas:使用Series.str.isnumeric处理带有缺失值的isnumeric ,例如 pandas 中的所有文本函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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