简体   繁体   English

通过匹配Panda DataFrame在Panda DataFrame中查找匹配项

[英]Finding matches in Panda DataFrame by matching a Panda DataFrame

Trying to return the rows of an array where a column matches the values of another array. 尝试返回一个数组的行,其中一列与另一个数组的值匹配。

I start with elm value and call the nearby function to find other elms near elm based on searching the df table. 我从elm值开始,并根据搜索df表调用nearby函数在elm nearby查找其他elms。 Function returns result like this (all int): 函数返回如下结果(所有int):

    C1    C2    C3    C4
0  100    20    11     1

So I need to extract the information from elm_data_table that satisfies two conditions: 因此,我需要从满足两个条件的elm_data_table中提取信息:

1) The value in LC-col column matches LC value 1) LC-col列中的值与LC值匹配

2) The value in ELM column has to match each of the 4 values from other_elm 2) ELM列中的值必须与other_elm中的4个值other_elm

I'm expecting 4 rows of data from elm_data_table as I'm trying to find data for 4 values 我正在尝试从elm_data_table 4行数据,因为我正在尝试查找4个值的数据

Any tips? 有小费吗?

import panda as pd

#df and elm_data_table are Panda dataframes

def nearby(elm, df):
    return df[df['ELM'] == elm].iloc[:,5:9]

elm = 1000
LC = 200

other_elm = nearby(elm, df)

other_elm_info = elm_data_table[(elm_data_table['LC-col'] == LC) & (elm_data_table['ELM'] == other_elm )]

Do you mean like this? 你是这个意思吗

import pandas as pd
import numpy as np

df = pd.DataFrame({ 'A' : 1.,
                    'B' : pd.Timestamp('20130102'),
                    'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
                    'D' : np.array([3] * 4,dtype='int32'),
                    'E' : pd.Categorical(["test","train","tot","toast"]),
                    'F' : 'foo' })

elms=['test','train']

df[df.E.isin(elms)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM