熊猫方法跨多个具有相同索引的列应用功能?

时间:2020-01-29 23:46:43

标签: python pandas numpy pandas-groupby numpy-ufunc

我有一个基本的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.dbar.c? -现在在DataFrame级别上,我想要每个索引存储一个名为result的新列,其结果如何?

0 个答案:

没有答案