在一列中使用 .isin() 函数测试的替代值(针对pandas(python))

2024-04-29 10:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑两个数据帧:

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('|')

And.在此处应用(元组):

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

但不匹配。你知道吗


Tags: columnsappledataframenamesredcolorpdbanana
2条回答

我想你需要:

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]
import pandas as pd
import numpy as np
df1 = pd.DataFrame(['green apple and banana are sweet fruits','how fresh is the banana','cherry from japan'],columns=['fruits_names'])
df2 = pd.DataFrame([['green apple|opal apple','red'],['banana|cavendish banana','yellow'],['cherry','black']],columns=['fruits','colors'])
df2['sep_colors'] = np.where(df2['fruits'], (df2['fruits'].str.split(pat='|')), df2['fruits'])


dic = dict(zip(df2['colors'].tolist(),df2['sep_colors'].tolist()))

final = []
for row in range(len(df1.fruits_names)):
    list1 = []
    for key, value in dic.items():
        for item in value:
            if item in df1.iloc[row][0]:
                list1.append(key)
    final.append(list1)

df1['colors'] = final

相关问题 更多 >