问题描述
我正在使用熊猫读取csv文件.数据是数字,但以文本形式存储在csv文件中.当某些值不正确或缺失时,它们是非数字的.如何过滤这些值并将剩余数据转换为整数.
I am using pandas to read a csv file. The data are numbers but stored in the csv file as text. Some of the values are non-numeric when they are bad or missing. How do I filter out these values and convert the remaining data to integers.
我认为有一种比遍历所有值并使用isdigit()
测试它们是否为数字的方法更好/更快的方法.
I assume there is a better/faster way than looping over all the values and using isdigit()
to test for them being numeric.
pandas或numpy是否可以识别读者中的不良价值?如果没有,最简单的方法是什么?我必须指定dtype才能使它起作用吗?
Does pandas or numpy have a way of just recognizing bad values in the reader? If not, what is the easiest way to do it? Do I have to specific the dtypes to make this work?
推荐答案
pandas.read_csv
具有参数na_values
:
na_values : list-like, default None
List of additional strings to recognize as NA/NaN
您可以在其中定义这些错误的值.
where you can define these bad values.
这篇关于在python pandas 中读取csv并处理错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!