在处理熊猫索引时,我一直在使用isin方法,它总是返回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)发布于 2017-06-05 10:31:12
使用str.contains并传递一个regex模式:
In[5]: df.index.str.contains('01|02')
Out[5]: array([ True, True, False], dtype=bool)isin查找精确匹配,这就是为什么返回所有False数组的原因。
发布于 2017-06-05 11:19:14
在您的示例中,df.index.isin(['01', '02'])方法检查索引中值的每个是否为等于与范围内的值之一的(类似于SQL)。
因此,在您的例子中,检查是:'N01'=='01' or 'N01' == '02',它是False
在您的情况下,正确使用.isin()应该是:
from pandas import DataFrame
df = DataFrame(data=[['a', 1], ['b', 2], ['c', 3]], index=['N01', 'N02', 'N03'])
df.index.isin(['N01', 'N02'])这将导致array([True, True, False], dtype=bool)
https://stackoverflow.com/questions/44366968
复制相似问题