Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Weighted average with multiple weights and groups
I am a beginner in Python and I am trying to improve my code - so I would appreciate some advice on how to improve the efficiency of the following.

I have the following dataset:

petdata = {
    'animal' : ['dog', 'cat', 'fish'],
    'male_1' : [0.57, 0.72, 0.62],
    'female_1' : [0.43, 0.28, 0.38],
    'age_01_1': [0.10,0.16,0.15],
    'male_2' : [0.57, 0.72, 0.62],
    'female_2' : [0.43, 0.28, 0.38],
    'age_01_2': [0.10,0.16,0.15],
    'weight_1': [10,20,30],

df = pd.DataFrame(petdata) 

I want to calculate a weighted average for the animals in my dataset using weight_1 for all the variable that end with "_1" and weight_2 for all the variables that end with "_2".

I am doing it in this way at the moment:



And this is for every single column in my dataframe. I realise this is not very neat, so can anyone give me some advice on how to improve the process?

I have tried to:
  • reshape the data from wide to long
    define a function for the weighted average

But I was unsuccessful with both. The issue not in the reshaping, I can do that, but they I am not clear on how to apply the different weights to the different groups I have in my data.

Many thanks for any help.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to extract different data groups from multiple CSV files using python Rafiz 3 362 Jun-04-2019, 05:20 PM
Last Post: jefsummers
  Centralities for Weighted Networks fishbacp 0 366 Oct-15-2018, 11:20 PM
Last Post: fishbacp
  Pandas dataframe: sum of exponentially weighted correlation matrices per row vvvcvvcv 1 813 May-29-2018, 01:09 AM
Last Post: scidam

Forum Jump:

Users browsing this thread: 1 Guest(s)