Difficulty: Beginner
Pandas is the most widely used Python library for data manipulation and analysis. Built on top of NumPy, it provides two primary data structures: Series and DataFrame. These structures make it straightforward to load, clean, transform, and analyze data from virtually any source, whether it is a CSV file, a SQL database, an Excel spreadsheet, or a JSON API response. The name "Pandas" is derived from "Panel Data," a term used in econometrics for multi-dimensional structured datasets.
A Series is a one-dimensional labeled array capable of holding any data type: integers, floats, strings, or even Python objects. Think of it as a single column in a spreadsheet with an index on the left and values on the right. You create a Series using pd.Series(), passing in a list, a dictionary, or a NumPy array. When you pass a dictionary, the keys become the index labels automatically. Series supports vectorized operations, meaning you can add, subtract, multiply, or apply functions to the entire Series without writing explicit loops.
A DataFrame is a two-dimensional labeled data structure with columns of potentially different types. It is essentially a collection of Series that share the same index. You can think of it as an in-memory spreadsheet or SQL table. DataFrames are created using pd.DataFrame(), which accepts dictionaries of lists, lists of dictionaries, NumPy arrays, or other DataFrames. Each column in a DataFrame is a Series, and you can access individual columns using bracket notation or dot notation.
Pandas aligns data automatically by index labels. This means that when you perform operations between two Series or DataFrames, Pandas matches entries by their index labels rather than by their positional order. This is a powerful feature that prevents many common data alignment bugs. If a label exists in one structure but not the other, the result will contain NaN (Not a Number) for that position.
Installing Pandas is as simple as running pip install pandas. By convention, it is always imported as pd: import pandas as pd. This alias is so universal that any Python data professional will recognize pd immediately. Throughout this course, every code example begins with this import statement.
import pandas as pd
# Create a Series from a list
fruits = pd.Series(['Apple', 'Banana', 'Cherry', 'Date'])
print(fruits)
print("---")
print("Type:", type(fruits).__name__)
When you create a Series from a list, Pandas automatically assigns integer indices starting from 0. The dtype 'object' indicates string data. The Series displays its index on the left and values on the right.
import pandas as pd
# Create a Series from a dictionary
ages = pd.Series({'Alice': 25, 'Bob': 30, 'Charlie': 35})
print(ages)
print("---")
print("Alice's age:", ages['Alice'])
When creating a Series from a dictionary, the keys become index labels. You can then access values using these labels with bracket notation, similar to how you access dictionary values.
import pandas as pd
# Create a DataFrame from a dictionary of lists
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['Delhi', 'Mumbai', 'Pune']
}
df = pd.DataFrame(data)
print(df)
print("---")
print("Shape:", df.shape)
The most common way to create a DataFrame is from a dictionary where keys are column names and values are lists of equal length. The shape attribute returns a tuple of (rows, columns).
import pandas as pd
# Create a DataFrame from a list of dictionaries
records = [
{'Product': 'Laptop', 'Price': 50000, 'Quantity': 10},
{'Product': 'Mouse', 'Price': 500, 'Quantity': 100},
{'Product': 'Keyboard', 'Price': 1500, 'Quantity': 50}
]
df = pd.DataFrame(records)
print(df)
print("---")
print("Columns:", list(df.columns))
Each dictionary in the list becomes a row. The keys become column names. This format is common when working with JSON API responses, where each record is a dictionary.
Pandas Overview, Series, DataFrame, pd.Series(), pd.DataFrame()