gpt4 book ai didi

python - Pandas : if value in a dataframe contains string from another dataframe, 追加列

转载 作者:行者123 更新时间:2023-12-01 06:03:16 25 4
gpt4 key购买 nike

假设我有两个数据框 df1 和 df2。
如果 df1 的特定列的值包含 df2 的特定列中的字符串,我想将 df2 的某些列附加到 df1,否则为 NaN。

一个小例子:

import pandas as pd
df1 = pd.DataFrame({'col': ['abc', 'def', 'abg', 'xyz']})
df2 = pd.DataFrame({'col1': ['ab', 'ef'], 'col2': ['match1', 'match2'], 'col3': [1, 2]})

df1:
   col
0  abc
1  def
2  abg
3 xyz

df2:

  col1    col2 col3
0   ab  match1 1
1   ef  match2 2

我想:
   col   col2_match   col3_match
0  abc match1 1
1  def match2 2
2  abg match1 1
3 xyz NaN NaN

我设法以一种肮脏和低效的方式做到这一点,但在我的情况下,df1 包含大约 100K 行并且它需要永远......

提前致谢 !

编辑

有点脏,但可以相对较快地完成工作(但我仍然认为存在最聪明的方法......):
import pandas as pd
import numpy as np


df1 = pd.DataFrame({'col': ['abc', 'def', 'abg']})
df2 = pd.DataFrame({'col1': ['ab', 'ef'],
'col2': ['match1', 'match2'],
'col3': [1, 2]})


def return_nan(tup):
return(np.nan if len(tup[0]) == 0 else tup[0][0])


def get_indexes_match(l1, l2):
return([return_nan(np.where([x in e for x in l2])) for e in l1])


def merge(df1, df2, left_on, right_on):
df1.loc[:, 'idx'] = get_indexes_match(df1[left_on].values,
df2[right_on].values)
df2.loc[:, 'idx'] = np.arange(len(df2))
return(pd.merge(df1, df2, how='left', on='idx'))


merge(df1, df2, left_on='col', right_on='col1')

最佳答案

您可以像这样使用 python difflib 模块进行模糊匹配

import difflib 
difflib.get_close_matches
df1.col = df1.col.map(lambda x: difflib.get_close_matches(x, df2.col1)[0])

所以现在你的 df1 是
    col
0 ab
1 ef
2 ab

如果您希望保持 df1 不变,可以将其称为 df3。

现在你可以合并
merged = df1.merge(df2, left_on = 'col', right_on = 'col1', how = 'outer').drop('col1', axis = 1)

合并的数据框看起来像
    col col2    col3
0 ab match1 1
1 ab match1 1
2 ef match2 2

编辑:
如果没有像给出的新示例那样匹配,您只需在 lambda 中放置一个条件
df1.col = df1.col.map(lambda x: difflib.get_close_matches(x, df2.col1)[0] if difflib.get_close_matches(x, df2.col1) else x)

现在合并后你得到
    col col2    col3
0 ab match1 1
1 ab match1 1
2 ef match2 2
3 xyz NaN NaN

关于python - Pandas : if value in a dataframe contains string from another dataframe, 追加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42698281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com