What Is a DataFrame?
Learn what a DataFrame is, how labeled rows and columns work in pandas, key operations like filtering and groupby, and how it differs from a Series.
Expected Interview Answer
A DataFrame is a two-dimensional, table-like data structure with labeled rows and columns that can hold different data types per column, most commonly used in libraries like pandas (Python) and R.
Each column in a DataFrame acts like a named array or Series with a single data type, while rows are indexed and can be accessed by position or label. DataFrames support operations like filtering, grouping, joining, and aggregating, similar to a spreadsheet or SQL table but manipulated entirely in code. This makes them the standard structure for loading, cleaning, transforming, and analyzing tabular data in data science workflows, since they combine intuitive tabular semantics with fast, vectorized computation.
- Stores heterogeneous, labeled tabular data efficiently
- Supports fast vectorized operations across rows and columns
- Enables intuitive filtering, grouping, and joining like SQL
- Integrates directly with visualization and ML libraries
- Provides both label-based and position-based indexing
AI Mentor Explanation
A DataFrame is like the full scorebook of a match: each column is a labeled stat such as runs, balls, or overs, and each row is one player's entry, all viewable and filterable at once. A scorer who wants every batter with a strike rate above 150 simply filters the scorebook the way code filters a DataFrame's rows.
Structure of a pandas DataFrame
Columns (Series)
- name (string)
- age (int)
- city (string)
Rows (Index)
- row 0
- row 1
- row 2
Operations
- filter
- groupby
- merge/join
Step-by-Step Explanation
Step 1
Load data
Read data from a CSV, database, or API into a DataFrame using functions like read_csv or read_sql.
Step 2
Inspect structure
Check columns, data types, and shape to understand what you're working with.
Step 3
Index and select
Access rows and columns by label (.loc) or position (.iloc).
Step 4
Transform
Filter, group, aggregate, or join the DataFrame to reshape it for analysis.
Step 5
Export or model
Write the result to a file/database or feed it directly into a machine learning pipeline.
What Interviewer Expects
- Describes a DataFrame as two-dimensional, labeled, tabular data
- Knows columns can hold different types while each column is homogeneous
- Can distinguish .loc (label-based) from .iloc (position-based) indexing
- Understands DataFrames relate to Series (1D) as a container of them
- Can mention common operations: filter, groupby, merge
Common Mistakes
- Confusing a DataFrame with a plain 2D NumPy array (no labels/mixed types)
- Not knowing the difference between .loc and .iloc
- Assuming all columns must share the same data type
- Forgetting that DataFrame operations often return copies, not views
Best Answer (HR Friendly)
“A DataFrame is basically a spreadsheet inside your code — rows and labeled columns holding your data, which you can filter, sort, and analyze programmatically. It's the standard way data scientists work with tabular data in tools like Python's pandas library.”
Code Example
import pandas as pd
df = pd.DataFrame({
"name": ["Ada", "Grace", "Alan"],
"age": [36, 85, 41],
"city": ["London", "New York", "Manchester"],
})
# Label-based and position-based selection
print(df.loc[df["age"] > 40, "name"]) # filter by label
print(df.iloc[0]) # first row by position
print(df.groupby("city")["age"].mean()) # aggregate by groupFollow-up Questions
- What is the difference between a DataFrame and a Series?
- How do .loc and .iloc differ when selecting data?
- How would you merge two DataFrames on a common key?
- What happens under the hood when you use groupby?
- How does a pandas DataFrame differ from a Spark DataFrame at scale?
MCQ Practice
1. How many dimensions does a standard DataFrame have?
A DataFrame is a two-dimensional structure with rows and columns, like a table or spreadsheet.
2. Which indexing method selects data by label rather than integer position?
.loc selects rows and columns using labels, while .iloc uses purely integer-based positions.
3. Can different columns in a DataFrame hold different data types?
Each column in a DataFrame is a Series with its own dtype, so different columns can hold strings, integers, floats, etc.
Flash Cards
What is a DataFrame? — A two-dimensional, labeled, table-like data structure holding columns that can each have a different data type.
What is a single column of a DataFrame called in pandas? — A Series — a one-dimensional labeled array.
Difference between .loc and .iloc? — .loc selects by label, .iloc selects by integer position.
Name a common DataFrame operation for combining two tables. — merge() or join(), which combine DataFrames on a shared key, similar to a SQL join.