简体   繁体   English

如何删除 python 中具有非数字数据的行

[英]how to remove the rows which has non-numeric data in python

Amount column consists of texts金额栏由文本组成

在此处输入图像描述

I have a column which is supposed to be an integer which is saved as object in the DataFrame.我有一列应该是 integer,它在 DataFrame 中保存为 object。 I tried to remove the columns which are not numeric by using the below codes:我尝试使用以下代码删除非数字的列:

df[df.columns[8]] = df[df.columns[8]].apply(pd.to_numeric, errors='coerce').fillna(0).astype(float).astype(int).dropna()

But the code is not working.但是代码不起作用。 I tried to replace the texts with 0 but its not working.我试图用 0 替换文本,但它不起作用。

This will render string rows to NAs.这会将字符串行呈现给 NA。

df["Amount in USD"] = pd.to_numeric(df["Amount in USD"], errors='coerce')

Then just filter out rows which has NAs.然后只需过滤掉具有 NA 的行。

df = df[df["Amount in USD"].notnull()]

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

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