如何根据字符串包含合并两个数据框?

Jor*_*ris 6 python merge pandas

我有 2 个数据框,我想根据字符串包含的内容将它们合并到特定列上。这似乎是以下问题,但顺序不同:How to merge pandas on string contains?

import pandas as pd

df1 = pd.DataFrame({'Amount':[10, 20, 30], 'Description':['this is a text','this is another text','this is an important']})
df2 = pd.DataFrame({'Text':['another','important'], 'Category':['Another Category','Important Category']})

rhs = (df1.Description
          .apply(lambda x: df2[df2['Category']] if df2[df2['Text']] in str(x).lower() else None)
      )

(pd.concat([df1.Amount, rhs], axis=1, ignore_index=True)
 .rename(columns={0: 'Amount', 1: 'Category'}))
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

KeyError: "None of [Index(['another', 'important'], dtype='object')] are in the [columns]"
Run Code Online (Sandbox Code Playgroud)

这是由于 lambda 表达式而发生的。使用 df2[df2['Text']] 部分,我尝试迭代包含类别的数据框,但这不起作用。

小智 2

假设 df2 是一个独特的文本及其类别表,我想这可以工作。(假设 dfs 与您发布的一样)

join_map = {row['Text']:row['Category'] for ind,row in df2.iterrows()}

df1['Category'] = df1['Description'].apply(lambda x: [val for key,val in join_map.items() if key in x][0] if [val for key,val in join_map.items() if key in x] else None)
Run Code Online (Sandbox Code Playgroud)