简体   繁体   English

部分字符串与 str.contains 匹配,使用 dict comprehension with python/pandas

[英]Partial string match with str.contains using dict comprehension with python/pandas

I have a data frame with a column called course_names with a mapping dictionary with a list of course names i'd like to match in the key and the value I would want to assign in the value column我有一个数据框,其中包含一个名为course_names的列, course_names包含一个映射字典,其中包含我希望在键中匹配的课程名称列表以及我希望在值列中分配的值

import pandas as pd
df = pd.DataFrame({'course_name' : ['Phsyics, Maths','Algebra & Maths','History','Geology','Biology']})


mapping = {'Algebra & Maths' : 'Mathematics',
'Phsyics' : 'Science',
'History' : 'History',
'Geology' : 'Geology',
'Biology' : 'Science'} 
# this goes on for about another 35 lines. 

#my attempt so far.

df['keys'] = [k for k, v in mapping.items() if df['course_name'] in k]

when I run this code I get :当我运行此代码时,我得到:

TypeError: 'in <string>' requires string as left operand, not Series

expected output :预期输出:

    course_name          key
0   Phsyics, Maths      Science
1  Algebra & Maths  Mathematics
2          History      History
3          Geology      Geology
4          Biology      Science

You had a small syntax error.你有一个小的语法错误。 You can also use map您也可以使用map

Try:尝试:

import pandas as pd
df = pd.DataFrame({"course_name" : ["Algebra & Maths", "Phsyics"]})
Mapping = {'Algebra & Maths' : 'Mathematics','Phsyics' : 'Science'}
df["keys"] = [v for k, v in Mapping.items() if k in df['course_name'].tolist()]
#df["keys"] = df["course_name"].map(Mapping)
print(df)

Output:输出:

       course_name         keys
0  Algebra & Maths  Mathematics
1          Phsyics      Science

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM