Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Count Distinct Combinations of two columns and add to Same Dataframe

Need help in adding the unique combination of two columns to the same dataframe in pandas. I want that "nos" column.

Input:
id  acct_nos name
1   1a       one
1   1a       two
2   2b       three
3   3a       four
3   3b       five
3   3c       six
3   3d       seven

Here is the output I want:

Output:
id  acct_nos    nos name
1   1a          1   one 
1   1a          1   two
2   2b          1   three
3   3a          4   four
3   3b          4   five
3   3c          4   six
3   3d          4   seven

In the above example Id=1 has only 1 acct_nos-1a so the nos has to have a value 1. Id=3 has only 4 acct_nos-3a to 3d so the nos has to have a value 4.

Not sure how to put this in Python Pandas. SQL queries I can figure out.

Thanks

like image 230
Arpit Avatar asked Apr 16 '17 21:04

Arpit


People also ask

How do I get two column combinations in pandas?

By use + operator simply you can combine/merge two or multiple text/string columns in pandas DataFrame. Note that when you apply + operator on numeric columns it actually does addition instead of concatenation.

How do I get unique values from two columns in pandas?

You can get unique values in column (multiple columns) from pandas DataFrame using unique() or Series. unique() functions. unique() from Series is used to get unique values from a single column and the other one is used to get from multiple columns.

How do you count unique occurrences in pandas?

You can use the nunique() function to count the number of unique values in a pandas DataFrame.

How do you count the number of occurrences of a value in a DataFrame?

Using the size() or count() method with pandas. DataFrame. groupby() will generate the count of a number of occurrences of data present in a particular column of the dataframe.


1 Answers

You can use groupby.transform with nunique() function to count the number of unique elements per id:

df['nos'] = df.groupby("id")['acct_nos'].transform("nunique")
df

enter image description here

like image 179
Psidom Avatar answered Nov 14 '22 22:11

Psidom