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
#2
Hey , I guess you should use group by, if you had provided a sample of dataset, I could have tried it out first, but anyways i guess this should work.
df2.groupby('Doc_Set').nunique()
Also here is a pandas guide if you don't know how groupby works Pandas
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Camelot not able to change column header shortmeister1 2 796 Mar-22-2025, 04:47 AM
Last Post: Pedroski55
  Transform 3 Columns into Single Column DaveG 9 3,598 Mar-19-2025, 03:46 AM
Last Post: robbert23
  renaming a column without a name in a dataframe Carbonpony 2 1,003 Jan-23-2025, 08:20 AM
Last Post: Carbonpony
  Updating column name with translation bobbydave 0 1,183 Sep-17-2024, 03:40 PM
Last Post: bobbydave
  Assigning cycle values in a list nmancini 3 1,018 Sep-16-2024, 09:35 PM
Last Post: deanhystad
  adding a calculated column charles986 1 913 Jun-13-2024, 05:31 PM
Last Post: deanhystad
  adding a calculated column charles986 1 696 Jun-13-2024, 02:27 PM
Last Post: deanhystad
  Adding PD DataFrame column bsben 2 1,345 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 2,026 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Help copying a column from a csv to another file with some extras g0nz0uk 3 1,776 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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