繁体   English   中英

检查 pandas dataframe 中的列值是否为数字

[英]Check if a column value is numeric in pandas dataframe

我有一个要清理的数据集。 数据集由 54 列和 315 行组成。 对于其中一列,我想找出该列中的所有值是否都是数字。 我做了以下事情:

work_sheet = pd.read_excel('2006_sale.xlsx', sheet_name='Sheet1')
df = work_sheet.copy()

试一试

for idx,val in enumerate(df['LotArea']):
    if(not(str(val).isnumeric())):        # Check if a value is numeric or not
        df.at[idx,'LotArea'] = np.nan     # If the value is not numeric then replace it with null

尝试 2

for idx,val in enumerate(df['LotArea']):
    if(not(isinstance(val,float))):        # Check if a value is numeric or not
        df.at[idx,'LotArea'] = np.nan     # If the value is not numeric then replace it with null

LotArea 的样本值为: 在此处输入图像描述

这两种方法的问题不知何故它将每个值检测为非数字,我最终的 output 看起来像这样: 在此处输入图像描述

知道我哪里出错了吗?

不需要 for 循环来实现这一点。 您可以使用 pd.to_numeric 方法并将错误设置为“强制”,所有非数字值都将替换为 NaN。

df['LotArea'] = pd.to_numeric(df['LotArea'], errors='coerce') 

首先我想把这个链接放在这里。 pandas 中的 for-loop 是反模式,并且有许多高性能方式可以在不使用 for-loop 的情况下实现数据转换。 请检查链接。

https://stackoverflow.com/a/55557758/2956135

要回答您的问题,请使用正则表达式replace function。

df['LotArea'] = df.LotArea.replace(regex='|[^\d+]', value=np.nan)

暂无
暂无

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

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