简体   繁体   English

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

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

I have a dataset that I want to clean.我有一个要清理的数据集。 The data set consists of 54 columns and 315 rows.数据集由 54 列和 315 行组成。 For one of the columns, I want to find whether all the values in that column are numeric or not.对于其中一列,我想找出该列中的所有值是否都是数字。 I have done the following:我做了以下事情:

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

TRY 1试一试

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

TRY 2尝试 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

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

Problem with both the approach Somehow it is detecting each value as non-numeric and my final output looks like this:这两种方法的问题不知何故它将每个值检测为非数字,我最终的 output 看起来像这样: 在此处输入图像描述

Any idea where i am going wrong?知道我哪里出错了吗?

A for loop is not needed to achieve this.不需要 for 循环来实现这一点。 You can use the pd.to_numeric method and by setting errors to 'coerce', all non-numeric values will be replaced with NaN.您可以使用 pd.to_numeric 方法并将错误设置为“强制”,所有非数字值都将替换为 NaN。

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

first I would like to drop this link here.首先我想把这个链接放在这里。 for-loop in pandas is anti-pattern and there are many performant way to achieve data transformation without using the for-loop. pandas 中的 for-loop 是反模式,并且有许多高性能方式可以在不使用 for-loop 的情况下实现数据转换。 Please check the link.请检查链接。

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

To answer your question, use replace function with a regex.要回答您的问题,请使用正则表达式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