gpt4 book ai didi

python - 使用 .isin() pandas (python) 测试的列中的替代值

转载 作者:行者123 更新时间:2023-12-01 00:19:05 25 4
gpt4 key购买 nike

考虑两个数据框:

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]]

问题是“fruits”列是否有替代值,例如:

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

将其转换为字典

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/

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