使用pandas模式的Python coulmns验证

使用pandas模式的Python coulmns验证,python,pandas,dataframe,validation,customvalidator,Python,Pandas,Dataframe,Validation,Customvalidator,我正在尝试使用PandasSchema验证我的DataFrame Coulmn。我一直在验证某些列,例如: 1.ip_地址-应包含以下格式1.1.1.1的ip地址,或者如果任何其他值出现错误,则应为null。 2.初始日期-格式为yyyy-mm-dd h:m:s或mm-dd-yyyy h:m:s等。 3.customertype应该在['type1'、'type2'、'type3']中,否则会引发错误。 4.客户满意=是/否或空白 5.客户ID不得超过5个字符,例如-cus01、cus02 6.

我正在尝试使用PandasSchema验证我的DataFrame Coulmn。我一直在验证某些列,例如:

1.ip_地址-应包含以下格式1.1.1.1的ip地址,或者如果任何其他值出现错误,则应为null。 2.初始日期-格式为yyyy-mm-dd h:m:s或mm-dd-yyyy h:m:s等。 3.customertype应该在['type1'、'type2'、'type3']中,否则会引发错误。 4.客户满意=是/否或空白 5.客户ID不得超过5个字符,例如-cus01、cus02 6.时间应采用%:%:格式或h:m:s格式。任何其他格式都会引发异常

from pandas_schema import Column, Schema
def check_string(sr):
    try:
        str(sr)
    except InvalidOperation:
        return False
    return True
def check_datetime(self,dec):
        try:
            datetime.datetime.strptime(dec, self.date_format)
            return True
        except:
            return False
def check_int(num):
    try:
        int(num)
    except ValueError:
        return False
    return True
    

string_validation=[CustomElementValidation(lambda x: check_string(x).str.len()>5 ,'Field Correct')]
int_validation = [CustomElementValidation(lambda i: check_int(i), 'is not integer')]
contain_validation = [CustomElementValidation(lambda y: check_string(y) not in['type1','type2','type3'], 'Filed is correct')]
date_time_validation=[CustomElementValidation(lambda dt: check_datetime(dt).strptime('%m/%d/%Y %H:%M %p'),'is not a date
 time')]
null_validation = [CustomElementValidation(lambda d: d is not np.nan, 'this field cannot be null')]

schema = Schema([
                 Column('CompanyID', string_validation + null_validation),
                 Column('initialdate', date_time_validation),
                 Column('customertype', contain_validation),
                 Column('ip', string_validation),
                 Column('customersatisfied', string_validation)])
errors = schema.validate(combined_df)
errors_index_rows = [e.row for e in errors]
pd.DataFrame({'col':errors}).to_csv('errors.csv')

我刚刚看了Pandashema的文档,如果不是所有的话,那么大部分都在寻找它的开箱即用功能。看看:

作为解决问题的快速尝试,以下几点应该可以奏效:

from pandas_schema.validation import (
    InListValidation
    ,IsDtypeValidation
    ,DateFormatValidation
    ,MatchesPatternValidation
)

schema = Schema([
    # Match a string of length between 1 and 5
    Column('CompanyID', [MatchesPatternValidation(r".{1,5}")]),

    # Match a date-like string of ISO 8601 format (https://www.iso.org/iso-8601-date-and-time-format.html)
    Column('initialdate', [DateFormatValidation("%Y-%m-%d %H:%M:%S")], allow_empty=True),
    
    # Match only strings in the following list
    Column('customertype', [InListValidation(["type1", "type2", "type3"])]),

    # Match an IP address RegEx (https://www.oreilly.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html)
    Column('ip', [MatchesPatternValidation(r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}")]),

    # Match only strings in the following list    
    Column('customersatisfied', [InListValidation(["yes", "no"])], allow_empty=True)
])

您好,您能告诉我另一种方法来解决这个问题,而不使用任何包,如Pandaschema。我需要它而不使用任何第三方软件包。如果它基本上是在循环或条件上工作,这将是很好的。您的问题表明您正在使用第三方库,如果您有不同的要求,请发布另一个问题。我想不使用pandas schema包来完成。使用简单的函数和条件语句。这是我的问题的链接