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

时间:2016-01-28 23:46:44

标签: python arrays pandas data-structures match

尝试返回列匹配另一个数组值的数组行。

我从elm值开始,并调用nearby函数,根据elm表搜索df附近的其他榆树。函数返回像这样的结果(所有int):

    C1    C2    C3    C4
0  100    20    11     1

所以我需要从满足两个条件的elm_data_table中提取信息:

1)LC-col列中的值与LC值匹配

2)ELM列中的值必须与other_elm

中的4个值相匹配

我期待来自elm_data_table的4行数据,因为我试图找到4个值的数据

任何提示?

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

1 个答案:

答案 0 :(得分:1)

你的意思是这样吗?

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