1

I have a dataset I am trying to group by some common values and then sum up some other values. The tricky part is I want to add some sort of weighting that keeps the largest number, I'll try to elaborate more below:

I've created a dummy data frame that is along the lines of my data just for example purposes:

df = pd.DataFrame({'Family': ['Contactors', 'Contactors', 'Contactors'], 
                   'Cell': ['EP&C', 'EXR', 'C&S'],
                    'Visits': ['25620', '626', '40']})

This produces a table like so:

enter image description here

So, in this example I would want all of the 'Contactors' to be grouped up by EP&C (as this has the highest visits to start with) but I would like all of the visits summed up and the other 'Cell' values dropped, so I would be left with something like this:

enter image description here

Could anyone advise?

Thanks.

1 Answer 1

2

IIUC, you can use:

(df
 # convert to numeric
 .assign(Visits=pd.to_numeric(df['Visits']))
 # ensure the top row per group is the highest visits
 .sort_values(by=['Family', 'Visits'], ascending=False)
 # for groups per Family
 .groupby('Family', sort=False, as_index=False)
 # aggregate per group: Cell (first row, i.e top) and Visits (sum of rows)
 .agg({'Cell': 'first', 'Visits': sum})
)

output:

       Family  Cell  Visits
0  Contactors  EP&C   26286
Sign up to request clarification or add additional context in comments.

7 Comments

Brilliant, thank you! If you wouldn't mind, could you explain your code a bit more? I'm unfamiliar with .agg for example.
Hi @mozway, may I ask: is it possible to use inplace=true here to store the new dataframe? I am having trouble with this.
@Stuquan given the different shape, not directly, what would be the expected output?
I'd want to export this output into a .csv file.
I meant the explicit data, not the storage ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.