[英]Find index values of a DataFrame that are between the index values of two other DataFrames
I have a dataframe : 我有一个数据框:
Index Id Event
1 1 A
2 1 B
3 1 A
4 1 B
5 1 A
6 1 B
7 1 A
8 1 B
9 1 A
10 1 B
11 1 A
I filter the dataframe based on some condition and got these 2 dataframe, df2 我根据某种条件过滤了数据框,并得到了这两个数据框df2
Index Id Event
4 1 B
9 1 A
and df3 和df3
Index Id Event
7 1 A
11 1 A
I want to find the rows that lies between the index of both data-frame and for each Id. 我想找到位于两个数据帧的索引和每个ID之间的行。
Index Id Event
5 1 A
6 1 B
10 1 B
You can construct an IntervalIndex
and query it efficiently. 您可以构造一个IntervalIndex
并对其进行有效查询。
# Setup - "Index" is a column(!).
df
Index Id Event
0 1 1 A
1 2 1 B
2 3 1 A
3 4 1 B
4 5 1 A
5 6 1 B
6 7 1 A
7 8 1 B
8 9 1 A
9 10 1 B
10 11 1 A
idx = pd.IntervalIndex.from_arrays(df1['Index'], df2['Index'], closed='neither')
df[idx.get_indexer(df['Index']) > -1]
Index Id Event
4 5 1 A
5 6 1 B
9 10 1 B
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.