info() for column types in Data Analysis Python - Time & Space Complexity
We want to understand how the time to run info() on a data table changes as the table grows.
Specifically, how does checking column types scale with more rows and columns?
Analyze the time complexity of the following code snippet.
import pandas as pd
n = 10 # example value for n
df = pd.DataFrame({
'A': range(n),
'B': [str(i) for i in range(n)],
'C': [float(i) for i in range(n)]
})
df.info()
This code creates a table with n rows and 3 columns, then calls info() to show column types and counts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each column to check data types and count non-null values.
- How many times: For each of the 3 columns, it looks through all
nrows once.
As the number of rows n grows, the time to check each column grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 checks (3 columns x 10 rows) |
| 100 | About 300 checks |
| 1000 | About 3000 checks |
Pattern observation: The work grows linearly with the number of rows.
Time Complexity: O(n)
This means the time to run info() grows in a straight line as the number of rows increases.
[X] Wrong: "The time to run info() depends mostly on the number of columns, not rows."
[OK] Correct: While columns matter, info() checks every row in each column, so more rows mean more work.
Understanding how data size affects analysis speed helps you write efficient code and explain your choices clearly.
"What if the DataFrame had 100 columns instead of 3? How would the time complexity change?"
Practice
DataFrame.info() method primarily show about a dataset?Solution
Step 1: Understand the purpose of
Theinfo()info()method is used to display concise summary of a DataFrame including data types and non-null counts.Step 2: Compare with other methods
Methods likehead()show rows,describe()shows statistics, andunique()shows unique values, so they are not correct here.Final Answer:
The data types of each column and non-null counts -> Option AQuick Check:
info()= data types + counts [OK]
- Confusing info() with head()
- Thinking info() shows statistics
- Assuming info() lists unique values
info() method on a DataFrame named df?Solution
Step 1: Recall method call syntax in Python
Methods are called with parentheses after the method name, sodf.info()is correct.Step 2: Check other options
info(df)is incorrect because info is not a standalone function;df.infomisses parentheses and returns the method object;df.info[]is invalid syntax.Final Answer:
df.info() -> Option DQuick Check:
Method call needs parentheses [OK]
- Forgetting parentheses when calling methods
- Using info() as a standalone function
- Using brackets [] instead of parentheses ()
df and its info() output, what is the data type of the column Age?
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Score': [85.5, 90.0, 88.0]}
df = pd.DataFrame(data)
df.info()
Output:
<class 'pandas.core.frame.DataFrame'> RangeIndex: 3 entries, 0 to 2 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Name 3 non-null object 1 Age 3 non-null int64 2 Score 3 non-null float64 dtypes: float64(1), int64(1), object(1) memory usage: 200.0 bytes
Solution
Step 1: Locate the column
The info output showsAgein the info outputAgehas 3 non-null values and data typeint64.Step 2: Match the data type to options
Among the options,int64matches the data type shown forAge.Final Answer:
int64 -> Option CQuick Check:
Age column type = int64 [OK]
- Confusing object with string
- Mixing float64 and int64 types
- Ignoring the dtype column in info()
df.info() but get an error: AttributeError: 'list' object has no attribute 'info'. What is the most likely cause?Solution
Step 1: Analyze the error message
The error says a 'list' object has no attribute 'info', meaning the variable is a list, not a DataFrame.Step 2: Identify the cause
Only DataFrames have theinfo()method. Calling it on a list causes this error.Final Answer:
You called info() on a list, not a DataFrame -> Option BQuick Check:
info() needs DataFrame, not list [OK]
- Assuming info() works on lists
- Ignoring error message details
- Thinking empty DataFrame causes this error
df with 200 columns. Column 'B' has some missing values. You want to quickly check which columns have missing data and their data types. Which code snippet using info() helps you do this?Solution
Step 1: Recall
With 200 columns (> pandas default max_info_columns=100),info()behavior on wide DataFramesdf.info()truncates the column list, hiding non-null counts and dtypes for most columns including potentially 'B'.Step 2: Check options
df.info(verbose=True)forces full display of all columns' non-null counts and dtypes. Other parameters likenull_countsandshow_missingare invalid.Final Answer:
df.info(verbose=True) -> Option AQuick Check:
verbose=True for full column info on large DFs [OK]
- Using df.info() on wide DFs (truncates columns)
- Using invalid parameters with info()
- Confusing verbose with missing data display
Ready to prove it?
Pass the test and this pattern moves unknown
