python - I want to print the occurance count of column3 in front of it as shown below -



python - I want to print the occurance count of column3 in front of it as shown below -

this question has reply here:

how print count of occourance of string in same csv file using python? 4 answers

i have 3 columns separated commas shown below(column1,column2,column3). in below illustration "241682-27638-usd-ocof" not repeating count one, "241942-37190-usd-div" repeated twice count 2 , on.

column1,column2,column3 ,occcurance_count_of_column3 name1,empid1,241682-27638-usd-ciggnt ,1 name2,empid2,241682-27638-usd-ocggint ,1 name3,empid3,241942-37190-usd-ggdiv ,2 name4,empid4,241942-37190-usd-chyof ,1 name5,empid5,241942-37190-usd-eqpl ,1 name6,empid6,241942-37190-usd-int ,1 name7,empid7,242066-15343-usd-cyjof ,3 name8,empid8,242066-15343-usd-cyjof ,3 name9,empid9,242066-15343-usd-cyjof ,3 name10,empid10,241942-37190-usd-ggdiv ,2 name11,empid11,242066-33492-usd-cjhof ,1

i have column1,column2,column3 in csv file. want have occcurance_count_of_column3 next column.i want check whether element in column3 repeats , how many times(occcurance_count). , print count of occurrence in same csv file using python.

you need counter. counter available stdlib's collections module can w/o. need 2 passes on data, , assume you're able slurp file content in list of lists info construction conveniently name table

counts = {} row in table: # utilize `get` method of dict optional `d` argument # set 0 (see ">>> help(dict.get)" if new you) counts[row[2]] = counts.get(row[2],0) + 1 row in table: print formatter(row,counts[row[2]])

python python-2.7 csv count

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -