我有一个有四列的大型df.其中一列包含单词,另一列将这些单词作为词典的关键字.我需要添加另一个专栏,提取感兴趣单词的值.示例:
ID ID2 words dict1
x12_12 12984 apple {'apple': 5, 'pear': 10}
x12_12 12984 pear {'apple': 5, 'pear': 10}
x12_12 20934 orange {'orange': 5, 'pear': NaN}
x12_12 20934 pear {'orange': 5, 'pear': NaN}
我需要创建一个名为Value的新列来从DICE1中提取信息
ID ID2 words dict1 value
x12_12 12984 apple {'apple': 5, 'pear': 10} 5
x12_12 12984 pear {'apple': 5, 'pear': 10} 10
x12_12 20934 orange {'orange': 20, 'pear': NaN} 20
x12_12 20934 pear {'orange': 20, 'pear': NaN} NaN
我有这个代码,它给了我想要的结果,但它需要很长时间才能运行,而且我有一个非常大的数据集.我知道‘应用’对于 Big Data 来说并不是最有效的.
df['value'] = df.apply(lambda row: row['dict1'][row['words']], axis=1)
有没有更快的方法?我try 使用np.vectorize,但它有一个问题与nan值,我不断得到错误.