0

I have a basic Pandas DataFrame, something like:

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 |

How do I do math per category per index? - To exemplify with one, say at index 2 I want to multiply can.c by foo.d and divide by can.d plus bar.c? - Now at the DataFrame level, I want a new column called result stored per index, with the result to this?

A T
  • 13,008
  • 21
  • 97
  • 158

0 Answers0