熊猫-行之间的比率

时间:2019-01-15 12:24:21

标签: python python-3.x pandas pandas-groupby sklearn-pandas

我需要找出相对于该列中某些值的列元素的比例。例如,在此表A中,我想找出关于该列{ id1 = x和id2 = z }的值的列 Metric 的比率。有人可以帮我吗?

例如:

表A

+-------+------+-------+
| id1   | id2  | metric|
+-------+------+-------+
| x     |  z   | 100   |
| x     |  w   | 10    |
+-------+------+-------+

正确的结果:

表B

+-------+------+-------+-------+
| id1   | id2  | metric| result|
+-------+------+-------+-------+
| x     |  z   | 100   |  1    | (100/100)
| x     |  w   | 10    |  0.1  | (10/100)
+-------+------+-------+-------+

代码:

d = {'id1': ['x', 'x'], 'id2': ['z','w'], 'metric': [100,10] }
df = pd.DataFrame(data=d)
df

1 个答案:

答案 0 :(得分:2)

如果我对您的理解正确,则说明以下内容:

设置

d = {'id1': ['x', 'x'], 'id2': ['z','w'], 'metric': [100,10] }
df = pd.DataFrame(data=d)
df

解决方案

# Manually choose the value by which to scale the column 'metric'
scaler = df.loc[(df['id1'] == 'x') & (df['id2'] == 'z'), 'metric'].values

# Divide all 'metric' values by the above scaler value
df['result'] = df['metric'] / scaler

df
  id1 id2  metric  result
0   x   z     100     1.0
1   x   w      10     0.1