list.index
works by checking equality of your input with elements from the list and looking for True
. It then returns the index of the first match.
Testing equality of 2 dataframes returns a dataframe:
df1 = pd.DataFrame([[1, 2]])
df2 = pd.DataFrame([[1, 2]])
print(df1 == df2)
0 1
0 True True
The truthness of the result is ambiguous:
print(bool(df1 == df2))
# ValueError: The truth value of a DataFrame is ambiguous.
# Use a.empty, a.bool(), a.item(), a.any() or a.all().
Option 1
In my opinion, the best way to check if you have the correct dataframe is to use an ordered dictionary and define keys (preferably, use descriptive names as keys):
from collections import OrderedDict
o = OrderedDict([(1, df1), (2, df2), (3, df3)])
print(list(o.keys()).index(2)) # 1
Option 2
Use a generator expression with is
, which returns True
only if 2 variables point to the same object:
lst = [df1, df2, df3]
res = next(i for i, j in enumerate(lst) if j is df2) # 1