Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group dataframe and get sum AND count?

I have a dataframe that looks like this:

              Company Name              Organisation Name  Amount 10118  Vifor Pharma UK Ltd  Welsh Assoc for Gastro & Endo 2700.00 10119  Vifor Pharma UK Ltd    Welsh IBD Specialist Group,  169.00 10120  Vifor Pharma UK Ltd             West Midlands AHSN 1200.00 10121  Vifor Pharma UK Ltd           Whittington Hospital   63.00 10122  Vifor Pharma UK Ltd                 Ysbyty Gwynedd   75.93 

How do I sum the Amount and count the Organisation Name, to get a new dataframe that looks like this?

              Company Name             Organisation Count   Amount 10118  Vifor Pharma UK Ltd                              5 11000.00 

I know how to sum or count:

df.groupby('Company Name').sum() df.groupby('Company Name').count() 

But not how to do both!

like image 770
Richard Avatar asked Jul 03 '16 20:07

Richard


People also ask

How do I sum multiple columns in pandas?

To sum given or list of columns then create a list with all columns you wanted and slice the DataFrame with the selected list of columns and use the sum() function. Use df['Sum']=df[col_list]. sum(axis=1) to get the total sum.

How to group by sum in pandas Dataframe?

Groupby sum in pandas dataframe python Groupby sum in pandas python can be accomplished by groupby() function. Groupby sum of multiple column and single column in pandas is accomplished by multiple ways some among them are groupby() function and aggregate() function.

How do you do groupby count in a Dataframe in Python?

Groupby count in pandas dataframe python Groupby count in pandas python can be accomplished by groupby() function. Groupby count of multiple column and single column in pandas is accomplished by multiple ways some among them are groupby() function and aggregate() function.

How to group by Count and reset_Index in a Dataframe?

We will groupby count with “Product” and “State” columns along with the reset_index () will give a proper table structure , so the result will be agg () function takes ‘count’ as input which performs groupby count, reset_index () assigns the new index to the grouped by dataframe and makes them a proper dataframe structure

How to count the number of rows in a Dataframe?

You can use pandas DataFrame.groupby ().count () to group columns and compute the count or size aggregate, this calculates a rows count for each group combination.


1 Answers

try this:

In [110]: (df.groupby('Company Name')    .....:    .agg({'Organisation Name':'count', 'Amount': 'sum'})    .....:    .reset_index()    .....:    .rename(columns={'Organisation Name':'Organisation Count'})    .....: ) Out[110]:           Company Name   Amount  Organisation Count 0  Vifor Pharma UK Ltd  4207.93                   5 

or if you don't want to reset index:

df.groupby('Company Name')['Amount'].agg(['sum','count']) 

or

df.groupby('Company Name').agg({'Amount': ['sum','count']}) 

Demo:

In [98]: df.groupby('Company Name')['Amount'].agg(['sum','count']) Out[98]:                          sum  count Company Name Vifor Pharma UK Ltd  4207.93      5  In [99]: df.groupby('Company Name').agg({'Amount': ['sum','count']}) Out[99]:                       Amount                          sum count Company Name Vifor Pharma UK Ltd  4207.93     5 
like image 149
MaxU - stop WAR against UA Avatar answered Sep 29 '22 20:09

MaxU - stop WAR against UA