Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas group by product instead of sum or count

In python pandas, I want to group a dataframe by column and then take the product of the rows for each ID. Sum and count functions exist, but a product?

df2 = pd.DataFrame({'X' : ['B', 'B', 'A', 'A'], 'Y' : [1, 2, 3, 4]})

print(df2.groupby(['X']).sum())
   Y
X   
A  7
B  3

print(df2.groupby(['X']).count())
   Y
X   
A  2
B  2

How can I take the product of the items instead of the sum or count?

print(df2.groupby(['X']).product())
   Y
X   
A  12
B  2
like image 843
Goofball Avatar asked Feb 17 '17 06:02

Goofball


People also ask

How do you group by and then sum in Pandas?

Use DataFrame. groupby(). sum() to group rows based on one or multiple columns and calculate sum agg function. groupby() function returns a DataFrameGroupBy object which contains an aggregate function sum() to calculate a sum of a given column for each group.

What is group by () in Pandas library?

Pandas groupby is used for grouping the data according to the categories and apply a function to the categories. It also helps to aggregate data efficiently. Pandas dataframe. groupby() function is used to split the data into groups based on some criteria.

How do I sum by group by?

You can sum values by group with one formula easily in Excel. Select next cell to the data range, type this =IF(A2=A1,"",SUMIF(A:A,A2,B:B)), (A2 is the relative cell you want to sum based on, A1 is the column header, A:A is the column you want to sum based on, the B:B is the column you want to sum the values.)

What does .values in Pandas do?

Definition and Usage The values property returns all values in the DataFrame. The return value is a 2-dimensional array with one array for each row.


1 Answers

There is prod:

df.groupby('X').prod()
like image 144
BrenBarn Avatar answered Oct 13 '22 01:10

BrenBarn