【问题标题】:Comparing columns with pandas_schema将列与 pandas_schema 进行比较
【发布时间】:2020-12-30 03:53:17
【问题描述】:

我正在使用 python 3.8 和 Pandas_schema 对数据运行完整性检查。我要求workflow_next_step 永远不能与workflow_entry_step 相同。我正在尝试生成一个比较两列的 CustomSeriesValidation,因为我没有看到执行此操作的股票函数。

有没有办法使用 Pandas_Schema 比较同一行中的两个单元格值?在此示例中,Pandas_Schema 将为 Mary 返回错误,因为她已从 In Progress 移动到 In Progress

df = config.pd.DataFrame({
'prospect': ['Bob', 'Jill', 'Steve', 'Mary'], 
'value': [10000, 15000, 500, 50000],
'workflow_entry_step': ['New', 'In Progress', 'Closed', 'In Progress'], 
'workflow_next_step': ['In Progress', 'Closed' ,None, 'In Progress']})

schema = Schema([
Column('prospect', [LeadingWhitespaceValidation(), TrailingWhitespaceValidation()]),
Column('value', [CanConvertValidation(int),'Doesn\'t convert to integer.']),
Column('workflow_entry_step', [InListValidation([None,'New','In Progress','Closed'])]),
Column('workflow_next_step', [CustomSeriesValidation(lambda x: x != Column('workflow_entry_step'), InListValidation([None,'New','In Progress','Closed'])]), 'Steps cannot be the same.')])

【问题讨论】:

    标签: python-3.x pandas dataframe validation schema


    【解决方案1】:
    import pandas as pd
    
    df = pd.DataFrame({
    'prospect': ['Bob', 'Jill', 'Steve', 'Mary'], 
    'value': [10000, 15000, 500, 50000],
    'workflow_entry_step': ['New', 'In Progress', 'Closed', 'In Progress'], 
    'workflow_next_step': ['In Progress', 'Closed' ,None, 'In Progress']})
    
    schema = Schema([
    Column('prospect', [LeadingWhitespaceValidation(), TrailingWhitespaceValidation()]),
    Column('value', [CanConvertValidation(float)]),
    Column('workflow_entry_step', [InListValidation([None,'New','In 
    Progress','Closed'])]),
    Column('workflow_next_step', [CustomSeriesValidation(lambda x: x != 
    df['workflow_entry_step'], 'Steps cannot be the same.'), 
    InListValidation([None,'New','In Progress','Closed'])])
    ])
    errors = schema.validate(df)
    for error in errors:
        print(error)
    

    输出:

    {row: 3, column: "workflow_next_step"}: "In Progress" Steps cannot be the same.
    

    【讨论】:

    • 该解决方案对我不起作用。 ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
    猜你喜欢
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    相关资源
    最近更新 更多