pandas isin函数
144浏览 · 2023-10-12 18:03:05
引用中提到了Pandas中isin()[[函数](https://geek.csdn.net/educolumn/2319d2a62911adc34b96ea572d8225a2?spm=1055.2569.3001.10083)](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)的源码实现和使用方法。isin()[[函数](https://geek.csdn.net/educolumn/2319d2a62911adc34b96ea572d8225a2?spm=1055.2569.3001.10083)](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)可以用来判断数据是否与多个值相等,若相等则返回True,否则返回False。具体使用方法如下:
import pandas as pd
# 创建一个DataFrame
data = pd.DataFrame([['foo','one','small',1],['foo','one','large',5],
['bar','one','small',10],['bar','two','small',10],
['bar','two','large',50]], columns=list('ABCD'))
# 示例 1: 判断B列中的值是否为one和two
data_1 = data['B'].isin(['one','two'])
# 示例 2: 判断data中的所有值是否为foo、small和two
data_2 = data.isin(['foo','small','two'])
# 示例 3: 判断A列中的值是否为foo,C列中的值是否为small或large,其他列返回False
data_3 = data.isin({"A":['foo'],'C':['small','large']})
引用中提到了isin()[函数](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)的使用示例,并且提到isin()还可以与loc方法连用,用来对数据进行筛选。例如,可以使用data[data['B'].isin(['one'])筛选出B列的值为one的数据。另外,引