Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: sum values from column to unique values

Tags:

python

pandas

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?

like image 872
Petr Petrov Avatar asked Sep 05 '17 16:09

Petr Petrov


People also ask

How do I sum specific values in pandas?

sum() function returns the sum of the values for the requested axis. Parameters: axis : {index (0), columns (1)}

How do you extract only unique values from a column in Python?

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.

What is the use of Nunique in pandas?

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.

What does sum () do in pandas?

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.


1 Answers

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
like image 194
Zero Avatar answered Sep 30 '22 16:09

Zero