Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Python
  3. Question
Python

Group by and Count in Python

Asked by Dadhija Raj Sep 25, 2024 653 views 1 answer
Share

About this question

For me the grouping is not working properly, I have mentioned my code and the result I require from it, I am trying to get results of the group by and count in python.

My Code:

#!/usr/bin/env python
counts = {}
logfile = open("/tmp/test.out", "r")
for line in logfile:
    if line.startswith("20") in line:
        seq = line.strip()
        substr = seq[0:13]
        if substr not in counts:
            counts[substr] = 0
            counts[substr] += 1
            for substr, count in counts.items():
                print(count,substr)

The result that I require as output:

6 2019-06-17T00
13 2019-06-17T01
9 2019-06-17T02
7 2019-06-17T03
6 2019-06-17T04


Your answer

1 Answer

More Python discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Python Blogs

Guides, tips and career advice on Python from JanBask experts.