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
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.
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.
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.)
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.
There is prod
:
df.groupby('X').prod()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With