使用包含列表的条件熊猫df进行过滤



我有一个df,它的单元格中有列表对象:

data['country_code']
0       [IT, IT]
1       [PL, PL]
2       [IT, IT]
3       [IT, IT]
4       [IT, IT]
...   
6318    [XX, MT]
6319    [FI, FI]
6320    [XX, XX]
6321    [FI, FI]
6322    [FI, FI]
Name: country_code, Length: 6323, dtype: object

如果data['country_code']中的列表有'SK''CZ'作为第一或第二元素,我想过滤数据框data

像这样:

data[first element of data['country_code'] == 'SK'or'CZ' or second element of data['country_code'] == 'SK'or'CZ']

在MongoDB的语法将是:

.find({$or: [{country_code: $elemMatch = 'SK'}, {country_code: $elemMatch = 'CZ'}]})

您可以使用:

print(df[df.country_code.apply(lambda x: "SK" in x or "CZ" in x)])

打印:

country_code
3     [IT, CZ]
4     [SK, IT]

dfused:

country_code
0     [IT, IT]
1     [PL, PL]
2     [IT, IT]
3     [IT, CZ]
4     [SK, IT]

您也可以使用pd.Series.straccessor:

l = ['SK', 'CZ']
print (data[data['country_code'].str[0].isin(l)|data["country"].str[1].isin(l)])

最新更新