1

When I want to check which Pandas dataframe is being called, how do I use the dataframe name in an if condition?

I keep getting "The truth value of a DataFrame is ambiguous...." and can't figure out a way around it.

df_even = pd.DataFrame( data=[2,4,6,12,64,866,222])
df_prime = pd.DataFrame( data=[5,7,11,13,17,19]) 
df_list = [df_even, df_prime]

for frame in df_list:
    if frame == df_even:
        name = 'even'
    elif frame == df_prime:
        name = 'prime'
    # do something with the dataframe next
Thom Rogers
  • 1,385
  • 2
  • 20
  • 33

1 Answers1

0

Since you're testing for identity, you should use the is opertator. Note the distinction between equality, i.e. the logical values of the two objects are the same, and identity, i.e. they are the same object, i.e., the two names refer to the same address in memory.

if frame is df_even:
    ...

Your original code isn't working because pandas overrides the == operator to perform a value-by-value comparison (rather than dataframe-level comparison). This special override is not returning a boolean as == ordinarily does.


More about == vs is.

shx2
  • 61,779
  • 13
  • 130
  • 153