假设我们有以下数据集:
import pandas as pd
data = [('apple', 'red', 155), ('apple', 'green', 102), ('apple', 'iphone', 48),
('tomato', 'red', 175), ('tomato', 'ketchup', 96), ('tomato', 'gun', 12)]
df = pd.DataFrame(data)
df.columns = ['word', 'rel_word', 'weight']
我想重新计算权重,使它们在每个组内总和为1.0(例如,在苹果和番茄中),并保持相关的权重不变(例如,苹果/红到苹果/绿仍应为155/102)。