Python / Data Science Essentials Interview Questions
What is a Pandas DataFrame and how does it differ from a NumPy array?
A Pandas DataFrame is a two-dimensional, labelled data structure — think of it as a spreadsheet or a SQL table in memory. Rows and columns both have labels (the index and the column names), and each column can hold a different data type. A Series is the single-column equivalent.
| Feature | NumPy ndarray | Pandas DataFrame |
|---|---|---|
| Dimensions | N-dimensional | Always 2-D (rows × columns) |
| Data type | Single dtype per array | Each column has its own dtype |
| Labels | Integer positions only | Named row index + column headers |
| Missing values | No native support (use np.nan) | First-class NaN / NaT / pd.NA |
| Primary use | Numerical computation | Tabular data: ETL, analysis, SQL-like ops |
import pandas as pd import numpy as np df = pd.DataFrame({ 'name': ['Alice', 'Bob', 'Carol'], 'age': [30, 25, 35], 'salary': [95000.0, 72000.0, np.nan], }) print(df.dtypes) # name object # age int64 # salary float64 print(df.shape) # (3, 3) print(df.index) # RangeIndex(start=0, stop=3, step=1) print(df.columns) # Index(['name', 'age', 'salary'], dtype='object')
DataFrames are built on top of NumPy arrays — each column is essentially a NumPy array wrapped with extra metadata. When computation speed is paramount you often drop down to df.values or df.to_numpy() to get the raw array and run NumPy operations on it.
More Related questions...