如何将列中的每个值除以该列子集的最大值
How to divide each value in column by the maximum value of a subset of that column
我试图将列中的每一行除以列中子列表的最大值,如果该列由类别变量过滤
Is there a single line vector equation that creates col3? I have been trying to use groupby with transform(lambda x: x...) but can't seem to get the effect of maxif where it only takes the max of col2 where col1 = the rows with the same category as the row in col2 being divided.
示例输入代码:
import pandas as pd
data = {'col1':['A', 'A', 'B', 'B'],
'col2':[1, 2, 3, 4]}
df = pd.DataFrame(data)
df
期望的输出:
col1
col2
col3
explanation
A
1
0.5
e.g. 1/2
A
2
1
e.g. 2/2
B
3
0.75
e.g. 3/4
B
4
1
e.g. 4/4
当然可以:
>>> df['col2'] / df.groupby('col1')['col2'].transform(max)
0 0.50
1 1.00
2 0.75
3 1.00
然后您可以将该结果分配给您选择的新列。
我试图将列中的每一行除以列中子列表的最大值,如果该列由类别变量过滤
Is there a single line vector equation that creates col3? I have been trying to use groupby with transform(lambda x: x...) but can't seem to get the effect of maxif where it only takes the max of col2 where col1 = the rows with the same category as the row in col2 being divided.
示例输入代码:
import pandas as pd
data = {'col1':['A', 'A', 'B', 'B'],
'col2':[1, 2, 3, 4]}
df = pd.DataFrame(data)
df
期望的输出:
col1 |
col2 |
col3 |
explanation |
---|---|---|---|
A |
1 |
0.5 |
e.g. 1/2 |
A |
2 |
1 |
e.g. 2/2 |
B |
3 |
0.75 |
e.g. 3/4 |
B |
4 |
1 |
e.g. 4/4 |
当然可以:
>>> df['col2'] / df.groupby('col1')['col2'].transform(max)
0 0.50
1 1.00
2 0.75
3 1.00
然后您可以将该结果分配给您选择的新列。