Home > Software engineering >  Pandas: How to multiply a index with value in a multi-index series?
Pandas: How to multiply a index with value in a multi-index series?

Time:10-22

I have a series as below,

Name     id    
A        1    10
         2    20
         3    30
         .    .
         .    .
B        1    100
         2    200
         3    300
         .
         .

I wanted to do something below,

pd.cut(series.id * series.value, bins=10)

How can I do this in a performant way?

CodePudding user response:

Combine Series.mul with Index.get_level_values:

s.mul(s.index.get_level_values('id'))
# `get_level_values` works both with "names" (i.e. `id`) and "index" (i.e. `1`)

Name  id
A     1      10
      2      40
      3      90
B     1     100
      2     400
      3     900
dtype: int64

So, you can feed that to pd.cut, of course:

pd.cut(s.mul(s.index.get_level_values('id')), bins=10)

Name  id
A     1       (9.11, 99.0]
      2       (9.11, 99.0]
      3       (9.11, 99.0]
B     1      (99.0, 188.0]
      2     (366.0, 455.0]
      3     (811.0, 900.0]
dtype: category
Categories (10, interval[float64, right]): [(9.11, 99.0] < (99.0, 188.0] < (188.0, 277.0] <
                                            (277.0, 366.0] ... (544.0, 633.0] < (633.0, 722.0] <
                                            (722.0, 811.0] < (811.0, 900.0]]
  • Related