Skip to main content
Back to problems
#1990
Medium Database

Count the number of experiments

Database
48.8% acceptance
Mar 31, 2026
27
203

No description available.

Solution

Pandas
Time O(1)
Space O(1)
LeetCode
solution.pandas
# Table: Experiments
# 
# +-----------------+------+
# | Column Name     | Type |
# +-----------------+------+
# | experiment_id   | int  |
# | platform        | enum |
# | experiment_name | enum |
# +-----------------+------+
# experiment_id is the column with unique values for this table.
# platform is an enum (category) type of values ('Android', 'IOS', 'Web').
# experiment_name is an enum (category) type of values ('Reading', 'Sports', 'Programming').
# This table contains information about the ID of an experiment done with a random person, the platform used to do the experiment, and the name of the experiment.
# 
#  
# 
# Write a solution to report the number of experiments done on each of the three platforms for each of the three given experiments. Notice that all the pairs of (platform, experiment) should be included in the output including the pairs with zero experiments.
# 
# Return the result table in any order.
# 
# The result format is in the following example.
#
# Example 1:
# Input:
# Experiments table:
# +---------------+----------+-----------------+
# | experiment_id | platform | experiment_name |
# +---------------+----------+-----------------+
# | 4             | IOS      | Programming     |
# | 13            | IOS      | Sports          |
# | 14            | Android  | Reading         |
# | 8             | Web      | Reading         |
# | 12            | Web      | Reading         |
# | 18            | Web      | Programming     |
# +---------------+----------+-----------------+
# Output:
# +----------+-----------------+-----------------+
# | platform | experiment_name | num_experiments |
# +----------+-----------------+-----------------+
# | Android  | Reading         | 1               |
# | Android  | Sports          | 0               |
# | Android  | Programming     | 0               |
# | IOS      | Reading         | 0               |
# | IOS      | Sports          | 1               |
# | IOS      | Programming     | 1               |
# | Web      | Reading         | 2               |
# | Web      | Sports          | 0               |
# | Web      | Programming     | 1               |
# +----------+-----------------+-----------------+
# Explanation:
# On the platform "Android", we had only one "Reading" experiment.
# On the platform "IOS", we had one "Sports" experiment and one "Programming" experiment.
# On the platform "Web", we had two "Reading" experiments and one "Programming" experiment.

import pandas as pd

def count_experiments(experiments: pd.DataFrame) -> pd.DataFrame:
  platforms = ['Android', 'IOS', 'Web']
  experiment_names = ['Reading', 'Sports', 'Programming']
  # Create all combinations
  from itertools import product
  all_combos = pd.DataFrame(list(product(platforms, experiment_names)), columns=['platform', 'experiment_name'])
  # Count actual experiments
  counts = experiments.groupby(['platform', 'experiment_name']).size().reset_index(name='num_experiments')
  result = all_combos.merge(counts, on=['platform', 'experiment_name'], how='left')
  result['num_experiments'] = result['num_experiments'].fillna(0).astype(int)
  return result