Pandas Replace дает мне странную ошибку - PullRequest
0 голосов
/ 05 июня 2018

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

1 Ответ

0 голосов
/ 05 июня 2018

Ваша логика кажется слишком сложной.Вам не нужно регулярное выражение, а pd.Series.replace неэффективно со словарем, даже если он может работать с серией списков.Вот альтернативный метод:

import pandas as pd
from io import StringIO

mystr = StringIO("""Course
English 21st Century
Maths in the Golden Age of History
Science is cool""")

df = pd.read_csv(mystr)

d = {'English' : 'Eng', 'Science' : 'Sci', 'Math' : 'Mat', 'History' : 'Hist'}

df['Interest'] = df['Course'].apply(lambda x: ', '.join([d[i] for i in d if i in x]))

print(df)

                               Course   Interest
0                English 21st Century        Eng
1  Maths in the Golden Age of History  Mat, Hist
2                     Science is cool        Sci
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...