3 回答
TA贡献1799条经验 获得超9个赞
尝试使用 isin(sports).all()
前任:
import pandas as pd
sports = ['tennis','cricket','swimming']
df = pd.DataFrame({'sport': ['tennis','cricket','swimming']})
print(df["sport"].isin(sports).all())
TA贡献1844条经验 获得超8个赞
您可以使用set.intersection
:
if not set(df['sport']).intersection(set(sports)): print('no, the sports arent in the column')
更有效地,您可以使用set.issubset
:
if not set(sports).issubset(set(df['sport'])): print('no, the sports arent in the column')
TA贡献1799条经验 获得超6个赞
我相信需要比较sets:
sports = ['tennis','cricket','swimming']
df = pd.DataFrame({'sport': ['tennis','cricket']})
print (df)
sport
0 tennis
1 cricket
print(set(df['sport']) >= set(sports))
False
df = pd.DataFrame({'sport': ['tennis','cricket','swimming']})
print (df)
sport
0 tennis
1 cricket
2 swimming
print(set(df['sport']) >= set(sports))
True
df = pd.DataFrame({'sport': ['tennis','cricket','swimming', 'another']})
print (df)
sport
0 tennis
1 cricket
2 swimming
3 another
print(set(df['sport']) >= set(sports))
True
添加回答
举报