Home > Software design >  Exception Report from pandas DataFrame
Exception Report from pandas DataFrame

Time:12-09

Background:
The following function takes a pandas DataFrame and renames it exceptions_df whilst applying 2x conditions to it.

Function:

def ownership_exception_report():
    df = ownership_qc()
    exceptions_df = df[df['Entity ID %'] != 100.00]
    exceptions_df = df[df['Account # %'] != 100.00]
    return exceptions_df

My problem:
Whilst my code works fine, I wonder if there is a simple and more eloquent way to apply 2x conditions to a DataFrame and resave it? At the moment I am simply resaving the exceptions_df twice and it seems rather messy. Or perhaps I am wrong, and this is the correct way to apply conditions to a DataFrame?

CodePudding user response:

def ownership_exception_report():
    df = ownership_qc()
    return df[(df['Entity ID %'] != 100.00) & (df['Account # %'] != 100.00)]

Or:

def ownership_exception_report():
    df = ownership_qc()
    return df[df['Entity ID %'].ne(100.00) & df['Account # %'].ne(100.00)]

Both will return a copy of df with only the rows where Entity ID % is 100 AND Account # % is 100.

  • Related