DataFrame列值与另一个DataFrame列匹配

DataFrame列值与另一个DataFrame列匹配

本文介绍了将Pandas DataFrame列值与另一个DataFrame列匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

country = []
for i in df_temp['Customer Name'].iloc[:]:
    if i in gui_broker['EXACT_DDI_CUSTOMER_NAME'].tolist():
        country.append(gui_broker["Book"].values[gui_broker['EXACT_DDI_CUSTOMER_NAME'].tolist().index(i)])
    else:
        country.append("No Book Defined")
df_temp["Country"] = country

我目前有一个带有一列(客户名称")的大型DataFrame(df_temp),并尝试将其与一个具有3列的小型DataFrame(gui_broker)进行匹配-其中之一具有大型DataFrame的所有唯一值("EXACT_DDI_CUSTOMER_NAME").

I have currently a large DataFrame (df_temp) with one column ('Customer Name') and am trying to match it with a small DataFrame (gui_broker) which has 3 columns - one of which has all unique values of the large DataFrame ('EXACT_DDI_CUSTOMER_NAME').

在匹配df_temp的值行之后,我想基于匹配在df_temp中创建一个新列,其值为我的小型DataFrame(gui_broker)的值'Book'.我尝试了每种apply lambda方法,但毫无头绪.上面提供的代码为我提供了一个解决方案,但是它很慢,而且不是像...那样的熊猫.

After matching the value row of df_temp I want to create a new column in df_temp with the value 'Book' of my small DataFrame (gui_broker) based on the matching. I tried every apply lambda method, but am out of clue. The above provided code provides me with a solution, but it's slow and not Pandas like...

我究竟该如何进行?

推荐答案

看起来像您在寻找join(文档为此处)通过将第一列中的选定列与第二列中的索引进行匹配,它将DataFrame与另一个DataFrame连接起来.

Looks like you are looking for join (docs are here)It joins DataFrame with the other by matching the selected column(s) in the first with the index in the second.

所以

df_temp.join(gui_broker
             .set_index("EXACT_DDI_CUSTOMER_NAME")
             .loc[:, ["Book"]],
             on="Customer Name")

这篇关于将Pandas DataFrame列值与另一个DataFrame列匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 15:41