Python Forum
Assigning Column nunique values to another DataFrame column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning Column nunique values to another DataFrame column
#1
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find duplicates in a pandas dataframe list column on other rows Calab 2 1,905 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,533 Aug-26-2024, 04:52 PM
Last Post: Calab
Question SOLVED: TTP match when final column may or may not be present Calab 1 988 Jul-03-2024, 02:45 PM
Last Post: Calab
  Create new column in dataframe Scott 10 3,341 Jun-30-2024, 10:18 PM
Last Post: Scott
  attempt to split values from within a dataframe column mbrown009 9 5,703 Jun-20-2024, 07:59 PM
Last Post: AdamHensley
  Putting column name to dataframe, can't work. jonah88888 2 3,204 Jun-18-2024, 09:19 PM
Last Post: AdamHensley
  Column Transformer with Mixed Types - sklearn aaldb 0 1,255 Feb-22-2024, 03:27 PM
Last Post: aaldb
  Assigning conditional values in Pandas Scott 3 2,124 Dec-19-2023, 03:10 AM
Last Post: Larz60+
  concat 3 columns of dataframe to one column flash77 2 2,075 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 2,563 Sep-29-2023, 05:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020