Basic DataFrame info (shape, dtypes, describe) in Data Analysis Python - Time & Space Complexity
We want to know how long it takes to get basic information from a DataFrame.
Specifically, how does the time grow when we ask for shape, data types, or summary statistics?
Analyze the time complexity of the following code snippet.
import pandas as pd
n = 1000
df = pd.DataFrame({
'A': range(n),
'B': range(n, 2*n),
'C': ['text']*n
})
shape = df.shape
types = df.dtypes
summary = df.describe()
This code creates a DataFrame and gets its shape, data types, and summary statistics.
- Primary operation: Calculating summary statistics with
df.describe()loops over columns and rows. - How many times: It processes each column and each row of numeric data once.
Getting shape and data types is very fast and does not depend on data size much.
Calculating summary statistics grows with the number of rows and columns.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (10 rows x 2 numeric columns) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The work grows roughly in proportion to the number of rows times numeric columns.
Time Complexity: O(n x m)
This means the time grows linearly with the number of rows and columns in the DataFrame.
[X] Wrong: "Getting the shape or data types takes a long time like processing all data."
[OK] Correct: Shape and data types are stored metadata, so accessing them is very fast and does not depend on data size.
Understanding how basic DataFrame info scales helps you work efficiently with data and explain your code clearly.
"What if we used df.describe(include='all') to include all columns? How would the time complexity change?"
Practice
df.shape tell you about a DataFrame?Solution
Step 1: Understand what
df.shapereturnsdf.shapegives a tuple with two numbers: rows and columns count.Step 2: Match the description to the options
The number of rows and columns in the DataFrame correctly describes this as the number of rows and columns.Final Answer:
The number of rows and columns in the DataFrame -> Option BQuick Check:
Shape = (rows, columns) [OK]
- Confusing shape with data types
- Thinking shape shows data summary
- Mixing shape with head() output
df?Solution
Step 1: Recall the attribute for column data types
The correct attribute to check data types isdf.dtypes, not a method.Step 2: Verify the options
df.dtypes usesdf.dtypescorrectly. Others are invalid or do not exist.Final Answer:
df.dtypes -> Option AQuick Check:
dtypes attribute shows column types [OK]
- Adding parentheses like df.dtypes()
- Using non-existent methods like df.types()
- Confusing describe() with dtypes
df below:import pandas as pd
data = {'A': [1, 2, 3], 'B': [4.0, 5.5, 6.1], 'C': ['x', 'y', 'z']}
df = pd.DataFrame(data)
print(df.describe())What will be the output of
df.describe()?Solution
Step 1: Understand what
It provides summary stats (count, mean, std, min, max, quartiles) for numeric columns only by default.df.describe()doesStep 2: Check the DataFrame columns
Columns A and B are numeric (int and float), C is string (non-numeric), so C is excluded.Final Answer:
Summary statistics for columns A and B only, excluding C -> Option CQuick Check:
describe() skips non-numeric columns by default [OK]
- Expecting describe() to include string columns
- Thinking describe() throws error on strings
- Assuming describe() shows only count
import pandas as pd
data = {'X': [10, 20, 30], 'Y': [5, 15, 25]}
df = pd.DataFrame(data)
print(df.dtypes())What is the cause of the error?
Solution
Step 1: Identify the error source
The code callsdf.dtypes()with parentheses, butdtypesis an attribute, not a method.Step 2: Understand correct usage
Correct usage isdf.dtypeswithout parentheses to access data types.Final Answer:
dtypes is an attribute, not a method, so parentheses cause error -> Option AQuick Check:
Use df.dtypes without () [OK]
- Adding parentheses to dtypes
- Assuming error is from missing import
- Thinking DataFrame is empty
df with columns: Age (int), Salary (float), and Department (string). You want to quickly check how many rows and columns it has, the data types of each column, and summary statistics for numeric columns. Which sequence of commands gives you all this information?Solution
Step 1: Identify commands for each info type
df.shapegives rows and columns count,df.dtypesshows data types, anddf.describe()gives summary stats for numeric columns.Step 2: Check options for correct sequence
df.shape; df.dtypes; df.describe()lists these three commands in order, so it provides all required info.Final Answer:
df.shape; df.dtypes; df.describe() -> Option DQuick Check:
shape, dtypes, describe cover size, types, stats [OK]
- Using df.columns instead of df.dtypes for types
- Confusing df.info() with df.describe()
- Missing shape to check size
Ready to prove it?
Pass the test and this pattern moves unknown
