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.
1
  • 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 5 hours ago

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.