Search code examples
pythonpandas

check if pandas df is empty with apply - python


I want to execute a function only when a df is not empty. Otherwise, the function should do nothing. if not df.empty: would work fine normally but I want to incorporate the same logic within a row wise function.

I need a row-wise function to send out rows as individual instances. However, I only want to send these instances when there are non-null values.

Using below, the row wise function will record 3 values, thereby executing the print statements. I'm aiming to perform a similar output to scenario 1.

import pandas as pd

df = pd.DataFrame({'Datetime': ['2020/01/30 14:00:00', '2020/01/30 14:00:00'],
           'Item': ['Apple','Apple'],
           'Cost': ['1','1'],
            })

df = df.drop_duplicates(keep = False)

Scenario 1:

if not df.empty:
    print(df['Datetime'])
    print(df['Item'])
    print(df['Cost'])
else:
    pass 

Scenario 2:

def func(df):

    print(len(df))

    if not df.empty:
        print(df['Datetime'])
        print(df['Item'])
        print(df['Cost'])
    else:
        pass 

df.apply(func, axis = 1)

Solution

  • Given that you want to send one email per row, you will invariably need to iterate over them.

    From your example dataframe, there aren't any "empty rows", so there's no need to skip anything row-wise. You can use iterrows and just call your function to send the email with the data from each row, accessing each column with the syntax row[col_name]. If df is empty, there simply won't be any loops and nothing will be executed.

    In the example below you can see how you can access the data in each row:

    for i, row in df.iterrows():
        print(f"sending email {i}:")
        print(f'{row['Datetime'] = }')
        print(f'{row['Item'] = }')
        print(f'{row['Cost'] = }')
    

    This will print:

    sending email 0:
    row['Datetime'] = '2020/01/30 14:00:00'
    row['Item'] = 'Apple'
    row['Cost'] = '1'
    sending email 1:
    row['Datetime'] = '2020/01/30 14:00:00'
    row['Item'] = 'Apple'
    row['Cost'] = '1'