0

II'm currently learning the Pandas library in Python (without AI assistance), and in one of my tasks I needed to count how many times each item appeared in a row of a DataFrame. Here's an example of the CSV I'm using:

ip,datetime,method,url,status,response_time
192.168.0.1,2025-11-16 08:00,GET,/home,200,123
10.0.0.2,2025-11-16 08:01,POST,/login,302,250
172.16.0.5,2025-11-16 08:01,GET,/products,200,87
192.168.0.3,2025-11-16 08:02,GET,/cart,500,105
10.0.0.4,2025-11-16 08:03,GET,/home,200,98
172.16.0.6,2025-11-16 08:04,POST,/checkout,201,210
192.168.0.7,2025-11-16 08:05,GET,/products,404,95
10.0.0.8,2025-11-16 08:06,GET,/home,200,110

Count how many times each URL is called.

New contributor
Mauricio Reisdoefer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • 1
    When you cannot get a program to work as you would like, we help you find where the problem is. But this site is not a code-writing service. So post your attempt at a solution. See minimal reproducible example. Commented Nov 17 at 21:07
  • @Booboo debugging questions are not the only type of question allowed on Stack Overflow; we also accept 'how to' questions like this, although we always appreciate some effort at an attempt on the OP's part. Commented 2 days ago
  • @TylerH Agreed. But then perhaps the question could have/should have been posted as a General advice / Other type of post Commented 2 days ago
  • @Booboo Maybe, but there's no requirement to post 'how to' questions as any second-class type of post. They're fine--and even encouraged--as traditional questions so long as they are sufficiently scoped and answerable. Usually, 'how to' questions tend to make better, more interesting Q&A references (the point of Stack Overflow) than debugging questions. Commented yesterday

1 Answer 1

2

You can count how many times each URL is called by using value_counts().

See the pandas value_counts documentation

import pandas as pd

data = {
    'ip': ['192.168.0.1', '10.0.0.2', '172.16.0.5', '192.168.0.3', '10.0.0.4', '172.16.0.6', '192.168.0.7', '10.0.0.8'],
    'datetime': ['2025-11-16 08:00', '2025-11-16 08:01', '2025-11-16 08:01', '2025-11-16 08:02',
                 '2025-11-16 08:03', '2025-11-16 08:04', '2025-11-16 08:05', '2025-11-16 08:06'],
    'method': ['GET', 'POST', 'GET', 'GET', 'GET', 'POST', 'GET', 'GET'],
    'url': ['/home', '/login', '/products', '/cart', '/home', '/checkout', '/products', '/home'],
    'status': [200, 302, 200, 500, 200, 201, 404, 200],
    'response_time': [123, 250, 87, 105, 98, 210, 95, 110]
}

df = pd.DataFrame(data)

url_counts = df['url'].value_counts()

print(url_counts)

url
/home        3
/products    2
/login       1
/cart        1
/checkout    1
Name: count, dtype: int64
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.