ValueError: The truth value of a GeoDataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

ValueError: The truth value of a GeoDataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

我有 DataFrame 并且我想做,这似乎是一个简单的步骤

import pandas as pd

# initialize list of lists
data = [['tom', 10], ['nick', 15], ['juli', 14]]
  
# Create the pandas DataFrame
mydf = pd.DataFrame(data, columns = ['Name', 'Age'])

f = mydf  

if(f == mydf or f == clinic):
    
    print("yes it is ok ")
else:
    print("no its not ok ")

为什么这个小程序会出现以下错误?

ValueError:DataFrame 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

以下不是最佳解决方案,但可以替代此问题:

# Get the name of a DataFrame 
def get_df_name(df):
    name =[x for x in globals() if globals()[x] is df][0]
    return name
f = doctors  ##  GeoDataFrame
f = get_df_name(f)

if(f == "doctors" or f == "clinic"):
    
    print("yes it is ok ")
else:
    print("no its not ok ")

输出:是的,没问题

我不能告诉你为什么,但是将两个数据帧与 == 进行比较似乎会触发此错误。您已经找到了避免这种情况的迂回方法,if globals()[x] is df 是关键部分 - the is in particular.

在不改变所有逻辑的情况下,做这个改变应该足够了:

import pandas as pd

# initialize list of lists
data = [['tom', 10], ['nick', 15], ['juli', 14]]
  
# Create the pandas DataFrame
mydf = pd.DataFrame(data, columns = ['Name', 'Age'])

f = mydf  

if(f is mydf or f is clinic):
    
    print("yes it is ok ")
else:
    print("no its not ok ")

然而,这只是一个创可贴解决方案;考虑这个修改:

import pandas as pd

# initialize list of lists
data = [['tom', 10], ['nick', 15], ['juli', 14]]
  
# Create the pandas DataFrame
mydf = pd.DataFrame(data, columns = ['Name', 'Age'])
mydf2 = pd.DataFrame(data, columns = ['Name', 'Age'])  # a separate but equal dataframe

f = mydf2  

if(f is mydf or f is clinic):
    
    print("yes it is ok ")
else:
    print("no its not ok ")  # output: not its not ok

真正的解决方案是使用 pandas.DataFrame.equals:

import pandas as pd

# initialize list of lists
data = [['tom', 10], ['nick', 15], ['juli', 14]]
  
# Create the pandas DataFrame
mydf = pd.DataFrame(data, columns = ['Name', 'Age'])
mydf2 = pd.DataFrame(data, columns = ['Name', 'Age'])

f = mydf2  

if(f.equals(mydf) or f.equals(clinic)):
    
    print("yes it is ok ")  # output: yes it is ok
else:
    print("no its not ok ")