LeetCode 2878 Get the Size of a DataFrame in Python | Pandas Tutorial for Beginners

Опубликовано: 14 Июнь 2026
на канале: JR: Educational Channel
74
1

Solve LeetCode 2878 "Get the Size of a DataFrame" in Python with this beginner-friendly Pandas tutorial! This problem asks you to return the dimensions (rows, columns) of a DataFrame, like one with columns `player_id` and `name`. We’ll use `pandas`’ `shape` attribute to get the size in one line, returning `[rows, columns]`. Perfect for Python learners, data science beginners, or anyone prepping for coding interviews with Pandas!

🔍 *What You'll Learn:*
Understanding LeetCode 2878’s requirements
Using `pandas`’ `shape` attribute to get DataFrame dimensions
Returning dimensions as a list `[rows, columns]`
Testing with example DataFrames

💻 *Code Used in This Video:*
import pandas as pd

def getDataframeSize(players: pd.DataFrame) - list[int]:
return [players.shape[0], players.shape[1]]

Test case
data = [[1, "Alice"], [2, "Bob"], [3, "Charlie"]]
df = pd.DataFrame(data, columns=["player_id", "name"])
size = getDataframeSize(df)
print(size) # Output: [3, 2] (3 rows, 2 columns)

Test with empty DataFrame
df_empty = pd.DataFrame(columns=["player_id", "name"])
size_empty = getDataframeSize(df_empty)
print(size_empty) # Output: [0, 2] (0 rows, 2 columns)

🌟 *Why Solve LeetCode 2878?*
This problem introduces you to `pandas`’ `shape` attribute, a key tool for data science and coding interviews! We’ll show how `players.shape` returns a tuple `(rows, columns)`, and how to format it as `[rows, columns]` for the problem. It’s a great way to learn DataFrame basics, with a time complexity of O(1) since `shape` is a direct attribute access. Master this, and you’ll be ready for more Pandas challenges!

📚 *Who’s This For?*
Python beginners learning Pandas
Data science enthusiasts working with DataFrames
Coders prepping for data-focused interviews

👍 Like, subscribe, and comment: What Pandas problem should we solve next? Next up: Pandas filtering—stay tuned!

#PythonTutorial #LeetCode2878 #PandasDataFrame #LearnPandas #DataScience