Python Forum

Full Version: Assigning Column nunique values to another DataFrame column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm having issues assigning an nunique value to a dataframe column.

Can't show real data but dataframe df is like below:

Doc_Set 	Doc_Num    Status
	
Set_123	    ED-0001    Complete
Set_123	    EG-0002    Complete
Set_123	    EG-0002    Complete 
Set_456	    EN-0010    In Progress
Set_456	    EN-0010    In Progress
Set_789	    ED-0500    Complete
I need another dataframe 'df2' with unique Doc_Set values and number of unique Doc_Num values for each, where Status = Complete. So df2 should look like:

Doc_Set 	Docs_Unique    
	
Set_123	        2  
Set_789	        1
I'm doing the following:

df2['Doc_Set'] = df[df['Status'] == 'Complete'].Doc_Set.unique()

for d in df2['Doc_Set']:
df2['Docs_Unique'] = df[df['Doc_Set']==d].Doc_Num.nunique()
Somehow every row in df2['Docs_Unique'] ends up as '5'.
Although, if I print(df[df['Doc_Set']==d].Doc_Num.nunique()) in the for loop, I get correct values.

What am I doing wrong?