pandas 指数isin方法 [英] Pandas index isin method

查看:53
本文介绍了 pandas 指数isin方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用pandas索引时,我无法使用isin方法,它总是返回False.

I'm having throuble working with the isin method when working with pandas indexes, it always returns False.

from pandas import DataFrame
df = DataFrame(data=[['a', 1], ['b', 2], ['c', 3]], index=['N01', 'N02', 'N03'])
df.index.isin(['01', '02'])

返回

array([False, False, False], dtype=bool)

推荐答案

使用 str.contains 并传递正则表达式模式:

Use str.contains and pass a regex pattern:

In[5]: df.index.str.contains('01|02')

Out[5]: array([ True,  True, False], dtype=bool)

isin 查找完全匹配,这就是为什么您返回所有 False 数组

isin looks for exact matches which is why you get all False array returned

这篇关于 pandas 指数isin方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆