简体   繁体   中英

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. Function returns result like this (all 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:

1) The value in LC-col column matches LC value

2) The value in ELM column has to match each of the 4 values from other_elm

I'm expecting 4 rows of data from elm_data_table as I'm trying to find data for 4 values

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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