我有一个包含日期的csv文件,国家等信息(只能在预定义列表中取值),我目前正在使用转换器参数进行一些健全性检查,如下所示:
import pandas as pd
def datesCheck(x):
#do some checks and return the formated date
pass
def countriesCheck(x):
if x in countriesList:
return x
else:
raise ValueError, x + ' is not a recognised country'
df = pd.read_csv('myCsvFile.csv', converters={'Dates': datesCheck, 'Countries': countriesCheck})
我想知道的是,如果有一种方法可以获得引发(或第一个)ValueError的行数(如果有的话),那么我可以打开csv文件并快速更改它。
感谢您的帮助。周末愉快!
答案 0 :(得分:0)
引入计数器变量并计算行并引发行导致错误:
import pandas as pd
current_row = 0
def datesCheck(x):
current_row += 1
#do some checks and return the formated date
# if date ok do nothing
# else raise ValueError, 'Error Row = ' + str(current_row )
pass
def countriesCheck(x):
if x in countriesList:
return x
else:
raise ValueError, x + ' is not a recognised country in row = ' + str(current_row)
df = pd.read_csv('myCsvFile.csv', converters={'Dates': datesCheck, 'Countries': countriesCheck})