Fix Python Pandas ValueError: The truth value of a DataFrame is ambiguous Error

By | June 30, 2021

When we are checking a DataFrame object is valid or not, ValueError: The truth value of a DataFrame is ambiguous will be reported. In this tutorial, we will introduce how to fix this error.

Look at example code below:

df_source = None
if os.path.exists(excel_name):
    df_source = pd.DataFrame(pd.read_excel(excel_name, sheet_name=sheet_name))
    print(df_source)
if df_source:
    df_dest = df_source.append(df)
    print(df_dest)

If df_source is valid, we will output it.

Run this code, we will get this result:

Fix Python Pandas ValueError - The truth value of a DataFrame is ambiguous

How to fix this value error?

We can use code below to fix.

if df_source is not None:
    df_dest = df_source.append(df)
    print(df_dest)

Leave a Reply