Python Pandas Module
Pandas is a fast, powerful, flexible, and easy-to-use open source data analysis and manipulation tool, built on top of the Python programming language.
Installation
pip install pandas
Importing the Module
import pandas as pd
Data Structures
Series
A one-dimensional labeled array capable of holding any data type.
s = pd.Series([1, 3, 5, 6, 8])
print(s)
DataFrame
A two-dimensional, size-mutable, potentially heterogeneous tabular data structure.
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35],
"City": ["New York", "Paris", "London"]
}
df = pd.DataFrame(data)
print(df)
Reading and Writing Data
Pandas supports reading from and writing to various file formats (CSV, Excel, SQL, JSON, etc.).
# Reading
df = pd.read_csv("data.csv")
# df_excel = pd.read_excel("data.xlsx")
# Writing
df.to_csv("output.csv", index=False)
# df.to_excel("output.xlsx", index=False)
Viewing Data
print(df.head()) # First 5 rows
print(df.tail()) # Last 5 rows
print(df.info()) # Index, Datatype and Memory information
print(df.describe()) # Summary statistics for numerical columns
print(df.columns) # Column names
Selection
By Column
ages = df["Age"]
By Label (.loc) and Position (.iloc)
# Selects rows by label/index
row = df.loc[0]
# Selects rows by integer position
first_row = df.iloc[0]
Filtering
# Rows where Age is greater than 28
adults = df[df["Age"] > 28]
Handling Missing Data
df.dropna() # Drop rows with missing values
df.fillna(value=0) # Fill missing values with 0
Grouping
# Group by a column and calculate mean
# df.groupby("City").mean()