Pandas выдает странный вывод при использовании словаря для замены значений внутри фрейма данных:
import pandas as pd
df = pd.read_csv('data.csv')
print(df)
Course
English 21st Century
Maths in the Golden Age of History
Science is cool
Mapped_Items = ['Math', 'English', 'Science', 'History']
pat = '|'.join(r"\b{}\b".format(x) for x in Mapped_Items)
df['Interest'] = df['Course].str.findall('('+ pat + ')').str.join(', ')
mapped_dict = {'English' : 'Eng', 'Science' : 'Sci', 'Math' : 'Mat', 'History' : 'Hist'}
df['Interest'] = df1['Interest'].replace(mapped_dict, inplace=False)
Что я получаю:
print(df)
df
Course Interest
English 21st Century Engg
Maths in the Golden Age of History MatttHistt
Science is cool Scii
То, что мне нужно, это что-то близкоена следующее:
Course Interests
English 21st Century Eng
Maths in the Golden Age of History Mat, Hist
Science is cool Sci