Python 使用字典键创建新列

Python 使用字典键创建新列,python,pandas,dataframe,Python,Pandas,Dataframe,获取此数据帧df: df=pd.DataFrame({'col_1':['some text green','some text blue','some text dog','some text']}) 拿这本字典来说: my_dict={'color':['green','blue'],'animal':['dog']} col_1 0 some text green 1 some text blue 2 some text dog 3 so

获取此数据帧
df

df=pd.DataFrame({'col_1':['some text green','some text blue','some text dog','some text']})

拿这本字典来说:

my_dict={'color':['green','blue'],'animal':['dog']}

             col_1
0  some text green
1   some text blue
2    some text dog
3        some text
如果字符串包含在
col\u 1
中,我需要创建一个新列
new\u col
进行搜索。字符串在字典值中给出。如果是这样,我需要获取dictionary键并将其放在新列中。如果没有,就把它放在南边

结果应该是:

             col_1 new_col
0  some text green   color
1   some text blue   color
2    some text dog  animal
3        some text     NaN
试试这个:

def f(s):
    for k in my_dict:
        for v in my_dict[k]:
            if v in s:
                return k

df["new_col"] = df["col_1"].apply(f)

它必须是一本字典吗?我们可以使用列表吗?我想要最简单的解决方案。可以是一份清单,是的。