【发布时间】:2017-09-26 06:22:49
【问题描述】:
假设我们有以下数据集:
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) .
【问题讨论】:
-
你能添加想要的输出吗?
-
请在单独的列中提及预期输出以便更好地理解