Find Median Given Frequency of Numbers: Leetcode Pandas
In data analysis, the median is a crucial statistical measure that represents the middle value of a dataset, effectively separating the higher half from the lower half. When dealing with frequency distributions, calculating the median requires decompressing the data based on the given frequencies. In this task, we’ll determine the median of all numbers in a database by decompressing a frequency table using Python’s Pandas library. The final median will be rounded to one decimal point.

The median is the value separating the higher half from the lower half of a data sample. Write a solution to report the median of all the numbers in the database after decompressing the Numbers
table. Round the median to one decimal point. The result format is in the following example.
Input:
Numbers table:
+-----+-----------+
| num | frequency |
+-----+-----------+
| 0 | 7 |
| 1 | 1 |
| 2 | 3 |
| 3 | 1 |
+-----+-----------+
Each row of this table shows the frequency of a number in the database.
Output:
+--------+
| median |
+--------+
| 0.0 |
+--------+
Explanation:
If we decompress the Numbers table,
we will get [0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 3],
so the median is (0 + 0) / 2 = 0.
import pandas as pd
data = [[0, 7],
[1, 1],
[2, 3],
[3, 1]]
numbers = pd.DataFrame(data,
columns=['num',
'frequency']).astype({'num':'Int64',
'frequency':'Int64'})
display(numbers)

Step 1. Decompressing the Numbers Table
- numbers[‘num’]: Select the num column from the numbers DataFrame.
- .repeat(numbers[‘frequency’]): Repeats each number in the num column according to its corresponding frequency.
- .to_frame(): Converts the resulting Series into a DataFrame.
df = numbers['num'].repeat(numbers['frequency']).to_frame()
display(df)

Step 2. Calculating the Median
- .median(): Computes the median of the num column in the DataFrame.
- .to_frame(‘median’): Converts the resulting median value into a DataFrame with the column name median.
- .round(1): Rounds the median value to one decimal place.
df = df.median().to_frame('median').round(1)
display(df)

Happy learning! Follow me on LinkedIn Instagram Medium profile or subscribe to my email list for the latest blogs, and click the clap button to support writing. Feel free to ask questions in the comment section. Don’t forget to share the blog link with your friends or LinkedIn connections.
References: [1] Leetcode: find median given frequency of numbers
[2] Full code in GitHub