Home > Software engineering >  Multiplying dataframes based on index value across all columns
Multiplying dataframes based on index value across all columns

Time:10-21

I have 2 dataframes with 'age' as the index on both. I want to use the multiplication rates in one df and apply these to the population in the other.

The multiplication rates by age are in a single column dataframe:

rate_for_yr = pd.DataFrame(np.array([[15,90],[16,80],[17,70]]),
                   columns=["Age","2020"])

rate_for_yr = rate_for_yr.set_index('Age')
Age 2020
15 90
16 80

The population data has various towns across columns:

pop_by_town = pd.DataFrame(np.array([[15, 1, 2, 3, 4, 5, 6], [16, 7, 8, 9, 10, 11, 12], [17, 13, 14, 15, 16, 17, 18]]),
                   columns=["Age","townA", "townB", "townC", "townD", "townE", "townF"])

pop_by_town= pop_by_town.set_index('Age')
Age townA townB townC
15 1 2 3
16 7 8 9

How can i apply the multipliers by age to the values in pop_by_town?

Expected output:

Age townA townB townC
15 90 180 270
16 560 640 720

I can't seem to land on anything that uses the index and just applies across all columns. Other solutions didn't like that the dfs were a different size...

CodePudding user response:

Since you already have the common index you can simply do a multiplication along row axis

pop_by_town.mul(rate_for_yr['2020'], axis=0)

     townA  townB  townC  townD  townE  townF
Age                                          
15      90    180    270    360    450    540
16     560    640    720    800    880    960
17     910    980   1050   1120   1190   1260
  • Related