I have dataframe
ID time coeff category
111 12 1.5 shop
111 15 1.5 shop
222 12 0.8 shop
222 18 0.8 shop
I need to get
category unique_users
shop 2.3
If I try
result = df.groupby(['category']).agg({'ID': pd.Series.nunique}).rename(columns={ 'member_id': 'unique_users'}).reset_index()
I get 2
, because it returns unique of df.ID
. How can I sum values in df.coeff
for unique df.ID
?
sum() function returns the sum of the values for the requested axis. Parameters: axis : {index (0), columns (1)}
array(list) and then use numpy. unique(x) function to get the unique values from the list. numpy. unique() returns only the unique values in the list.
Pandas DataFrame nunique() Method The nunique() method returns the number of unique values for each column. By specifying the column axis ( axis='columns' ), the nunique() method searches column-wise and returns the number of unique values for each row.
The sum() method adds all values in each column and returns the sum for each column. By specifying the column axis ( axis='columns' ), the sum() method searches column-wise and returns the sum of each row.
Here's one way
In [2314]: (df.groupby('category')
.apply(lambda x: x.groupby('ID').coeff.first().sum())
.reset_index(name='unique_users'))
Out[2314]:
category unique_users
0 shop 2.3
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