I have a CSV file in which contains one column (column1). I want to check whether the element in cell repeats and how many times(occcurance_count).And print count of occurrence in the same CSV file using Python.
In the below example the "241682-27638-USD-OCOF" is not repeating so the count is one, "241942-37190-USD-DIV" is repeated twice so the count is 2 and so on.
Want the output as below in CSV format
column1 ,occcurance_count
1682-27638-USD-OGGCOF ,1
241682-27638-USD-OGGINT ,1
241682-27638-USD-CIGGNT ,1
241682-27638-USD-OCGGINT ,1
241942-37190-USD-GGDIV ,2
241942-37190-USD-CHYOF ,1
241942-37190-USD-EQPL ,1
241942-37190-USD-INT ,1
242066-15343-USD-CYJOF ,3
242066-15343-USD-CYJOF ,3
242066-15343-USD-CYJOF ,3
242066-15343-USD-ETHQPL ,1
242066-15343-USD-INFRT ,1
241942-37190-USD-GGDIV ,2
242066-33492-USD-CJHOF ,1
You could use Counter:
As the count repeats you just need a normal dict:
If your file posted is actually expected output and you are trying to count each occurrence of a file with a single string on each line and the write the line and count:
I think below is the code which you are looking for. logic is simple but lengthier too. Explanation about logic: first you need to open csv file for reading and list down all elements in list Then use list count method to find out number of occurrence of each list item open the new csv file and write item and count for each item.
Surely there could be optimize way of doing the same thing but here is code which comes quickly.
Here is a simple code. Hope this will help you: