gpt4 book ai didi

python - 使用字典替换 Pandas 列中字符串中的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:34 31 4
gpt4 key购买 nike

我正在尝试使用 dictionary keypandas 列中的 strings 替换为其 >值。但是,每一列都包含句子。因此,我必须先对句子进行分词,并检测句子中的单词是否与我字典中的键对应,然后用相应的值替换字符串。

但是,结果就是我继续搞不定。有没有更好的 pythonic 方法来解决这个问题?

目前这是我的 MVC。在评论中,我指定了问题发生的位置。

import pandas as pd

data = {'Categories': ['animal','plant','object'],
'Type': ['tree','dog','rock'],
'Comment': ['The NYC tree is very big','The cat from the UK is small','The rock was found in LA.']
}

ids = {'Id':['NYC','LA','UK'],
'City':['New York City','Los Angeles','United Kingdom']}


df = pd.DataFrame(data)
ids = pd.DataFrame(ids)

def col2dict(ids):
data = ids[['Id', 'City']]
idDict = data.set_index('Id').to_dict()['City']
return idDict

def replaceIds(data,idDict):
ids = idDict.keys()
types = idDict.values()
data['commentTest'] = data['Comment']
words = data['commentTest'].apply(lambda x: x.split())
for (i,word) in enumerate(words):
#Here we can see that the words appear
print word
print ids
if word in ids:
#Here we can see that they are not being recognized. What happened?
print ids
print word
words[i] = idDict[word]
data['commentTest'] = ' '.apply(lambda x: ''.join(x))
return data

idDict = col2dict(ids)
results = replaceIds(df, idDict)

结果:

None

我正在使用python2.7,当我打印出dict时,有Unicode的u'

我的预期结果是:

类别

评论

类型

评论测试

  Categories  Comment  Type commentTest
0 animal The NYC tree is very big tree The New York City tree is very big
1 plant The cat from the UK is small dog The cat from the United Kingdom is small
2 object The rock was found in LA. rock The rock was found in Los Angeles.

最佳答案

您可以创建字典 然后replace :

ids = {'Id':['NYC','LA','UK'],
'City':['New York City','Los Angeles','United Kingdom']}

ids = dict(zip(ids['Id'], ids['City']))
print (ids)
{'UK': 'United Kingdom', 'LA': 'Los Angeles', 'NYC': 'New York City'}

df['commentTest'] = df['Comment'].replace(ids, regex=True)
print (df)
Categories Comment Type \
0 animal The NYC tree is very big tree
1 plant The cat from the UK is small dog
2 object The rock was found in LA. rock

commentTest
0 The New York City tree is very big
1 The cat from the United Kingdom is small
2 The rock was found in Los Angeles.

关于python - 使用字典替换 Pandas 列中字符串中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46342492/

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