Beginner
Introduction to Pandas: Series and DataFrames
📂 Phase 5: NumPy, Pandas, Data Processing Basics (Days 21–25) · Python ProgrammingPandas is the standard Python library for working with structured, tabular data — think spreadsheets, CSV files, and SQL query results. It is built directly on top of NumPy, and its two core data structures, Series and DataFrame, are what make real-world data analysis in Python practical.
Installing and Importing Pandas
# pip install pandas
import pandas as pd # "pd" is the universal, near-mandatory alias
The Series — A One-Dimensional Labeled Array
A Series is similar to a NumPy array, but every value also has an associated label, called its index — making it behave somewhat like a hybrid between a list and a dictionary.
import pandas as pd
scores = pd.Series([85, 90, 78, 92])
print(scores)
# 0 85
# 1 90
# 2 78
# 3 92
# dtype: int64
Series With a Custom Index
scores = pd.Series([85, 90, 78], index=["Math", "Science", "English"])
print(scores)
# Math 85
# Science 90
# English 78
# dtype: int64
print(scores["Science"]) # 90 — access by label, just like a dictionary key
The DataFrame — A Two-Dimensional Labeled Table
A DataFrame is the core pandas structure — a table with labeled rows and columns, where each column is internally a Series. This maps almost directly onto a spreadsheet or a SQL table.
data = {
"name": ["Vishwas", "Priya", "Arjun"],
"age": [22, 21, 23],
"city": ["Bengaluru", "Mumbai", "Chennai"]
}
df = pd.DataFrame(data)
print(df)
# name age city
# 0 Vishwas 22 Bengaluru
# 1 Priya 21 Mumbai
# 2 Arjun 23 Chennai
Creating a DataFrame From a List of Lists
rows = [
["Vishwas", 22, "Bengaluru"],
["Priya", 21, "Mumbai"]
]
df = pd.DataFrame(rows, columns=["name", "age", "city"])
print(df)
Reading Data From a CSV File
df = pd.read_csv("students.csv")
print(df.head()) # shows the first 5 rows by default
print(df.tail(3)) # shows the LAST 3 rows
read_csv() is one of the single most-used functions in all of pandas — real-world data almost always starts life as a CSV, Excel file, or SQL query result before becoming a DataFrame.
Inspecting a DataFrame
| Method/Attribute | Shows |
|---|---|
| df.shape | (rows, columns) as a tuple |
| df.columns | The list of column names |
| df.dtypes | The data type of every column |
| df.info() | A summary: column names, types, non-null counts |
| df.describe() | Statistical summary (mean, min, max, etc.) for numeric columns |
print(df.shape) # (3, 3) — 3 rows, 3 columns
print(df.columns) # Index(['name', 'age', 'city'], dtype='object')
print(df.describe()) # statistics for the 'age' column, since it's numeric
Accessing a Column
print(df["age"]) # returns a Series — the entire "age" column
print(df.age) # same result, dot notation (only works if the name has no spaces)
print(df[["name", "age"]]) # returns a DataFrame with just these two columns
Series vs DataFrame — At a Glance
| Aspect | Series | DataFrame |
|---|---|---|
| Dimensions | 1D (a single column of data) | 2D (rows and columns, like a table) |
| Relationship | A single column of a DataFrame IS a Series | Made up of one or more Series sharing a common index |
Interview tip: "What is the relationship between a Series and a DataFrame?" is asked frequently — the precise answer is that a DataFrame is essentially a collection of Series objects sharing the same index, with each individual column of a DataFrame technically being a Series in its own right.