[英]Add/fill pandas column based on range in rows from another dataframe
使用熊猫,我有 df1 按时间样本索引:
data = '''\
time flags input
8228835.0 53153.0 32768.0
8228837.0 53153.0 32768.0
8228839.0 53153.0 32768.0
8228841.0 53153.0 32768.0
8228843.0 61345.0 32768.0'''
fileobj = pd.compat.StringIO(data)
df1 = pd.read_csv(fileobj, sep='\s+', index_col='time')
df2 表示带有开始和结束的时间范围,以定义“检查”状态为 True 的范围:
data = '''\
check start end
20536 True 8228837 8228993
20576 True 8232747 8232869
20554 True 8230621 8230761
20520 True 8227351 8227507
20480 True 8223549 8223669
20471 True 8221391 8221553'''
fileobj = pd.compat.StringIO(data)
df2 = pd.read_csv(fileobj, sep='\s+')
我需要做的是向 df1 添加一列“检查”,并使用 True 值填写 df2 中定义的实际时间范围。 所有其他人都应该是假的。 一个示例结果是:
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
....
8228994.0. 12424.0. 32768.0. False
您可以创建一个列表或范围,然后将pd.Index.isin
与itertools.chain
pd.Index.isin
使用:
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
我认为您可以将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
使用any()
列表理解。 虽然不知道实际性能,但如果您能为我们运行 %timings 就好了!
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.