考虑两个数据框:

df1 = pd.DataFrame(['apple and banana are sweet fruits','how fresh is the banana','cherry from japan'],columns=['fruits_names'])
df2 = pd.DataFrame([['apple','red'],['banana','yellow'],['cherry','black']],columns=['fruits','colors'])


然后是代码:

colors =[]
for f in df1.fruits_names.str.split().apply(set):   #convert content in a set with splitted words

    color = [df2[df2['fruits'].isin(f)]['colors']]  #matching fruits in a list
    colors.append(color)


我可以轻松地在df1中插入颜色

df1['color'] = colors

output:
                    fruits_names            color
0  apple and banana are sweet fruits  [[red, yellow]]
1            how fresh is the banana       [[yellow]]
2                  cherry from japan        [[black]]


问题是“水果”列是否具有替代值,例如:

df2 = pd.DataFrame([[['green apple|opal apple'],'red'],[['banana|cavendish banana'],'yellow'],['cherry','black']],columns=['fruits','colors'])


如何保持此代码正常工作?

我最后尝试的是创建一个新的列,其中包含水果的单独值:

df2['Types'] = cf['fruits'].str.split('|')


和.apply(tuple)在这里:

color = [df[df['Types'].apply(tuple).isin(f)]['colors']]


但这不匹配。

最佳答案

我认为您需要:

print(df1)

    fruits_names
0   green apple and banana are sweet fruits
1   how fresh is the banana
2   cherry and opal apple from japan


使用splitdf.explode()

df2["fruits"] = df2["fruits"].apply(lambda x: x.split("|"))

df2 = df2.explode("fruits")

print(df2)


输出:

   fruits              colors
0   green apple        red
0   opal apple         red
1   banana             yellow
1   cavendish banana   yellow
2   cherry             black


将其转换为dict

d = {i:j for i,j in zip(df2["fruits"].values, df2["colors"].values)}


根据条件创建列

df1["colors"] = [[v for k,v in d.items() if k in x] for x in df1["fruits_names"]]

print(df1)


最终输出:

    fruits_names                            colors
0   green apple and banana are sweet fruits [red, yellow]
1   how fresh is the banana                 [yellow]
2   cherry and opal apple from japan        [red, black]

关于python - 用.isin() Pandas 测试的列中的替代值(python),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59086374/

10-12 20:22