简体   繁体   English

查找位于其他两个DataFrame的索引值之间的DataFrame的索引值

[英]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.

相关问题 根据索引替换两个数据框之间的列值 - Replace column values between two dataframes according to index Dataframe 到多个数据帧或列表以获取唯一索引值 - Dataframe to multiple dataframes or lists for unique index values 基于索引组合两个数据帧,替换其他列中的匹配值 - Combined two dataframe based on index, replacing matching values in other column 如何通过两个限制之间的列值索引数据框 - How to index a dataframe by a column's values between two limits 从其他数据帧创建新的 dataframe 但出现索引和值(Nan)问题 - creating new dataframe from other dataframes' but index and values(Nan) problem keep occurs 合并两个具有重叠索引的数据帧,保留左侧数据帧中的列值 - Merge two dataframes with overlapping index, keeping column values from left DataFrame Python:如果其他值在 DataFrame 之间匹配,则对 DataFrame 中的值求和 - Python: Sum values in DataFrame if other values match between DataFrames 两个 DataFrame,找到第二个的索引,其中两列的值从第一个匹配 - Two DataFrames, find index of second one where values of two columns match up from first 试图将两个数据帧彼此并置,但索引值和长度不匹配有些麻烦 - Trying to concat two dataframes on top of each other but having some troubles with index values and length mismatch 减去与其他数据帧中的索引匹配的值 - Subtract values matching index in other dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM