3 回答

TA贡献1847条经验 获得超11个赞
您可以创建一个列表或范围,然后pd.Index.isin与使用itertools.chain:
from itertools import chain
df2 = df2[df2['check']]
ranges = map(range, df2['start'], df2['end'])
df1['check'] = df1.index.isin(chain.from_iterable(ranges))
print(df1)
flags input check
time
8228835.0 53153.0 32768.0 False
8228837.0 53153.0 32768.0 True
8228839.0 53153.0 32768.0 True
8228841.0 53153.0 32768.0 True
8228843.0 61345.0 32768.0 True

TA贡献1828条经验 获得超3个赞
我想你可以使用IntervalIndex与loc
df2.index=pd.IntervalIndex.from_arrays(df2.start,df2.end,'both')
df2.loc[df.index]
Out[174]:
check start end
[1, 2] True 1 2
[4, 5] True 4 5
[7, 8] True 7 8
df['newcol']=df2.loc[df.index].check.values.tolist()
df
Out[176]:
flags input newcol
flags
2 2 32768.0 True
4 4 32768.0 True
7 7 32768.0 True

TA贡献1876条经验 获得超7个赞
使用的列表理解any()。但是,如果您可以为我们运行%timing,那么对实际性能没有任何了解,那就太好了!
df1['check'] = [any(start <= i <= end for start,end in
zip(df2['start'], df2['end'])) for i in df1.index]
print(df1)
返回值:
flags input check
time
8228835.0 53153.0 32768.0 False
8228837.0 53153.0 32768.0 True
8228839.0 53153.0 32768.0 True
8228841.0 53153.0 32768.0 True
8228843.0 61345.0 32768.0 True
添加回答
举报