I'm trying to create new column with the count of each attribute (Number) but it's given me the total count of row as shown below
POM = pd.read_excel('POM.xlsx' , sheet_name='Sheet1')
POM['Merged']=POM['ACD ID'].astype(str)+PG['Date'].astype(str)
POM['Count']=POM['Merged'].count()
print(POM.head(2))
POM.to_excel('Test.xlsx' , index=False)
Result
Date ACD ID Merged Count
0 2022-03-08 14004 140042022-03-08 986
1 2022-03-09 14004 140042022-03-09 986
How can I count the unique code which is repetition found in column (Merged)?
# Date ACD ID Merged Count
2022-03-08 00:00:00 14004 140042022-03-08 10
2022-03-09 00:00:00 14004 140042022-03-09 49
2022-03-10 00:00:00 14004 140042022-03-10 62
i have try the bellow code as answered & i found accurate answer but in same time the Merged New coulmn that i created by group(date+ ACD ID) are not accuaret as shown below , that date in ( merged ) not matched the date
POM = pd.read_excel('POM.xlsx' , sheet_name='Sheet1')
POM['Merged']=POM['ACD ID'].astype(str)+PG['Date'].astype(str)
#POM['Count'] = POM['Merged'].count()
POM['Count'] = POM['Merged'].copy().replace(POM['Merged'].value_counts().to_dict())
#POM['Count']=POM.groupby(["Merged"])["Merged"].transform("count")
#POM['counts'] = POM['Merged'].map(POM['Merged'].value_counts())
print(POM.head(6))
#POM.to_excel('Test.xlsx' , index=False)
result
Date ACD ID Merged Count
0 2022-03-08 14004 140042022-03-08 1
1 2022-03-08 14005 140052022-03-09 1
2 2022-03-08 14006 140062022-03-10 1
3 2022-03-08 14007 140072022-03-15 1
Try
groupby()
and thentransform("count")
:Check out this thread.