我有一个基本的Pandas DataFrame,类似:
In [2]: print(pd
.DataFrame({
'a': pd.Index([1,2,3, 1,2,3]),
'b': pd.Categorical(['can','foo','bar', 'can','foo','bar']),
'c': pd.Series([7,8,9, 10,5,60]),
'd': pd.Series([7,8,9, 20,40,5])
})
.set_index('a')
.to_markdown())
| a | b | c | d |
|----:|:----|----:|----:|
| 1 | can | 7 | 7 |
| 2 | foo | 8 | 8 |
| 3 | bar | 9 | 9 |
| 1 | can | 10 | 20 |
| 2 | foo | 5 | 40 |
| 3 | bar | 60 | 5 |
我如何对每个索引的每个类别进行数学计算? -例如,在索引2
上我想将can.c
乘以foo.d
,然后除以can.d
加bar.c
? -现在在DataFrame级别上,我想要每个索引存储一个名为result
的新列,其结果如何?