gpt4 book ai didi

python - Pandas Replace 给了我一个奇怪的错误

转载 作者:行者123 更新时间:2023-11-30 22:13:42 24 4
gpt4 key购买 nike

当使用字典替换数据框中的值时,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

最佳答案

你的逻辑似乎过于复杂。您不需要正则表达式,并且 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

关于python - Pandas Replace 给了我一个奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50704358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com