Python 如何跨具有相同索引的多个列应用函数?
我有一个基本的熊猫数据框架,类似于:Python 如何跨具有相同索引的多个列应用函数?,python,pandas,numpy,pandas-groupby,numpy-ufunc,Python,Pandas,Numpy,Pandas Groupby,Numpy Ufunc,我有一个基本的熊猫数据框架,类似于: 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.S
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
的新列,其结果是这样的?您能给出一个所需输出的示例吗?可能是我正在寻找的窗口函数?