熊猫-检查列表中的所有元素是否都在一列中

时间:2018-07-26 11:33:29

标签: python pandas

我有一个包含3列的数据框-'sport''age''name'

我有一个包含不同运动项目的列表,声明如下:

sports = ['tennis','cricket','swimming']

我要检查所有3种运动是否都在运动栏中。目前,我有这个:

if (df['sport']!=sport).any():
    print('no, the sports arent in the column')

因此,如果列表中的所有运动不在列中,我要打印否。我当前代码的问题是,运动列中的元素比运动列表中的元素多得多,所以给了我一个错误。

3 个答案:

答案 0 :(得分:3)

尝试使用isin(sports).all()

例如:

import pandas as pd
sports = ['tennis','cricket','swimming']
df = pd.DataFrame({'sport': ['tennis','cricket','swimming']})
print(df["sport"].isin(sports).all())

答案 1 :(得分:1)

您可以使用while

set.intersection

更有效地,您可以使用if not set(df['sport']).intersection(set(sports)): print('no, the sports arent in the column')

set.issubset

答案 2 :(得分:1)

我相信需要比较set

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