比较 df 列中的值,将匹配项提取到 1 列,将差异提取到另一列中



嗨,提前感谢,蟒蛇和熊猫的新手。

  1. 我有一个df列df['name'],这个大数据由产品名称组成,所有产品名称都有不同的长度,字母,数字,标点符号和间距。 这使得每个名称都是唯一的值,这使得很难找到某些产品的变体。

  2. 然后,我按间距拆分列值。

    df['name'].str.split(" ",expand = True)

我在这个问题中找到了一些代码,但我不知道如何应用它来迭代和比较列表,因为它使用变量和 2 个列表,我只有一个列表。 如何比较 python 中的两个列表和返回匹配项?

Not the most efficient one, but by far the most obvious way to do it is: a = [1, 2, 3, 4, 5] b = [9, 8, 7, 6, 5] set(a) & set(b) {5} if order is significant you can do it with list comprehensions like this: [i for i, j in zip(a, b) if i == j] [5]

  1. 我试图实现的是:

数据集

1.star t-shirt-large-red 2.star t-shirt-large-blue 3.star t-shirt-small-red 4.beautiful rainbow skirt small 5.long maxwell logan jeans- light blue -32L-28W 6.long maxwell logan jeans- Dark blue -32L-28W

-将列表中的所有项目相互比较,并找到最长的字符串匹配项。示例:产品:1,2,3具有匹配的部分字符串result COL1 COL2 COL3 COL4 1[star t-shirt] [large] [red] NONE 2[star t-shirt] [large] [blue] NONE 3[star t-shirt] [small] [red] NONE 4[beautiful rainbow skirt small] NONE NONE NONE 5[long maxwell logan jeans] [light blue] [32L] [28W] 6[long maxwell logan jeans] [Dark blue] [32L] [28W]

谁能指出我如何实现最终结果的正确方向。我研究了像fuzzywuzzy和diffilab这样的模块,但不知道如何应用它和正则表达式,但我不确定如何在具有这么多不同格式的列表中实现字符串匹配? 请在回复时逐步解释一下,以便我了解您的工作和原因。仅用于学习目的 再次提前感谢您。

嗯,你的问题真的很大。我认为你必须重新考虑这样做的目的。

在第一步中,每行彼此对应。

df['onkey'] = 1
df1 = pd.merge(df[['name','onkey']],df[['name','onkey']], on='onkey')
df1['list'] = df1.apply(lambda x:[x.name_x,x.name_y],axis=1)

第二步是找到最长的字符串匹配项。

from os.path import commonprefix
df1['COL1'] = df1['list'].apply(lambda x:commonprefix(x))

删除找不到字符串匹配项的行。

df1['COL1_num'] = df1['COL1'].apply(lambda x:len(x))
df1 = df1[(df1['COL1_num']!=0)]

查找最短的匹配项。

df1 = df1.loc[df1.groupby('name_x')['COL1_num'].idxmin()]

合并 df 和 df1。

df = df.rename(columns ={'name':'name_x'})
df = pd.merge(df,df1[['name_x','COL1']],on='name_x',how ='left')

我们可以像这样查看数据:

name_x  onkey                           COL1
0                         star t-shirt-large-red      1                  star t-shirt-
1                        star t-shirt-large-blue      1                  star t-shirt-
2                         star t-shirt-small-red      1                  star t-shirt-
3                  beautiful rainbow skirt small      1  beautiful rainbow skirt small
4  long maxwell logan jeans- light blue -32L-28W      1     long maxwell logan jeans- 
5   long maxwell logan jeans- Dark blue -32L-28W      1     long maxwell logan jeans-

如您所见,我们找到了最长的字符串匹配。

处理公共字符串,我们将剩余的字符串分开。

df['len'] = df['COL1'].apply(lambda x: len(x))
df['other'] = df.apply(lambda x: x.name_x[x.len:],axis=1)
df['COL1'] = df['COL1'].apply(lambda x: x.strip())
df['COL1'] = df['COL1'].apply(lambda x: x[:-1] if x[-1]=='-' else x)
df['other'] = df['other'].apply(lambda x:x.split('-'))

最后,我们将连接它们。

df = df[['COL1','other']]
df = pd.concat([df['COL1'],df['other'].apply(pd.Series)],axis=1)

结果:

COL1            0     1    2
0                   star t-shirt        large   red  NaN
1                   star t-shirt        large  blue  NaN
2                   star t-shirt        small   red  NaN
3  beautiful rainbow skirt small                NaN  NaN
4       long maxwell logan jeans  light blue    32L  28W
5       long maxwell logan jeans   Dark blue    32L  28W

最新更新