将字典重新应用到pandas数据框

将字典重新应用到pandas数据框

我没有一种非常蟒蛇的方法来做我想做的事,但是想知道是否没有一种更快的方法,因为这不能很好地扩展。

d = {'Text' : pandas.Series([['A','B'],['A','C'],['D']])}
Combined['Text'] = pandas.DataFrame(d)
word_model = {"A": 0.1, "B": 0.25, "C": 0.33, "D":1.01}
Combined['model_score'] = 0
for i in xrange(Combined.shape[0]):
    words = Combined['Text'][i]
    wordlist = words.split(' ')
    for j in xrange(len(wordlist)):
        Combined['model_score'][i] = Combined['model_score'][i] + word_model[wordlist[j]]
    Combined['model_score'][i] = Combined['model_score'][i]/(j+1)


这使我们的Combined ['model_score']取值:

0.175
0.215
1.01

最佳答案

当然,这是使用pd.Series.apply的另一种方式。

import pandas as pd

df = pd.DataFrame({'Text': [['A', 'B'], ['A', 'C'], ['D']]})

d = {'A': 0.1, 'B': 0.25, 'C': 0.33, 'D':1.01}

df['Avg Score'] = df['Text'].apply(lambda x: sum(map(d.get, x)) / len(x))

#      Text  Avg Score
# 0  [A, B]      0.175
# 1  [A, C]      0.215
# 2     [D]      1.010


如果需要将默认值设置为0.5(例如):

df['Avg Score'] = df['Text'].apply(lambda x: sum((d.get(i, 0.5) for i in x)) / len(x))

关于python - 将字典重新应用到pandas数据框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49413012/

10-13 01:29