Run in Google Colab
|
View on GitHub
|
Pandas Tutorial¶
A comprehensive yet beginner-friendly tutorial on pandas, a popular Python library for data manipulation and analysis.
We will cover:
Creating and loading data into a pandas
DataFrame.Basic indexing, merging, grouping, and computing statistics.
Modifying data with
.loc,.iloc, and using functions likevalue_counts().
1. Installation and Import¶
Install pandas (if not already installed):
!pip install pandas
Import pandas in Python:
import pandas as pd
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35],
"City": ["New York", "Los Angeles", "Chicago"]
}
df = pd.DataFrame(data)
print(df)
2.2. From a List of Dictionaries¶
data_list = [
{"Name": "Alice", "Age": 25, "City": "New York"},
{"Name": "Bob", "Age": 30, "City": "Los Angeles"},
{"Name": "Charlie", "Age": 35, "City": "Chicago"}
]
df2 = pd.DataFrame(data_list)
print(df2)
2.3. From CSV or Excel¶
Pandas makes it easy to read data from common file types:
df_csv = pd.read_csv("my_data.csv") # from CSV
df_excel = pd.read_excel("my_data.xlsx") # from Excel
#Replace `"my_data.csv"` with your actual file path or URL.
3. Basic Data Inspection¶
After creating or loading a DataFrame, you’ll often want to inspect it:
print(df.head()) # First 5 rows (use df.head(10) for first 10)
print(df.tail()) # Last 5 rows
print(df.shape) # (rows, columns)
print(df.columns) # List of column names
print(df.info()) # Summary of the DataFrame (types, non-null counts)
print(df.describe()) # Basic statistics for numeric columns
# Dot notation (for simple column names without spaces/special chars)
print(df.Age)
# Bracket notation
print(df["Age"])
4.2. Row Selection with .loc and .iloc¶
.locselects rows and columns by label..ilocselects rows and columns by integer position.
df = pd.DataFrame({
"Name": ["Alice", "Bob", "Charlie", "Dave"],
"Age": [25, 30, 35, 28],
"City": ["NY", "LA", "Chicago", "Seattle"]
}, index=["row1", "row2", "row3", "row4"]) # custom index labels
# Using .loc (label-based)
print(df.loc["row2"]) # Entire row labeled 'row2'
print(df.loc["row2", "Age"]) # Specific cell (row2, Age)
print(df.loc["row1":"row3"]) # Slice multiple rows by label
print(df.loc[:, ["Name", "City"]]) # All rows, only these columns
# Using .iloc (integer-based)
print(df.iloc[1]) # 2nd row (since indexing starts at 0)
print(df.iloc[1, 1]) # Cell in row index=1, col index=1
print(df.iloc[0:2]) # Rows 0 to 1
print(df.iloc[:, [0, 2]]) # All rows, columns 0 and 2
# Show only rows where Age > 28
mask = df["Age"] > 28
older_than_28 = df[mask]
print(older_than_28)
Multiple Conditions¶
Use bitwise operators & (AND), | (OR), and ~ (NOT):
# People older than 25 AND living in NY
df_filtered = df[(df["Age"] > 25) & (df["City"] == "NY")]
print(df_filtered)
df.loc["row1", "Age"] = 26
print(df)
6.2. Assigning with .iloc¶
df.iloc[0, 1] = 27
print(df)
6.3. Vectorized Assignments¶
# Increase everyone's Age by 1
df["Age"] = df["Age"] + 1
print(df)
print(df["Age"].mean()) # Average age
print(df["Age"].max()) # Max age
print(df["Age"].min()) # Min age
7.2. value_counts()¶
city_counts = df["City"].value_counts()
print(city_counts)
8. Grouping and Aggregation¶
.groupby() allows you to split data into groups based on some criteria, apply functions to each group, and combine results.
data = {
"Name": ["Alice", "Bob", "Charlie", "Dave"],
"Age": [25, 30, 35, 28],
"City": ["NY", "LA", "NY", "LA"],
"Salary": [70000, 80000, 120000, 95000]
}
df = pd.DataFrame(data)
# Group by 'City' and calculate mean Salary
grouped = df.groupby("City")["Salary"].mean()
print(grouped)
df_left = pd.DataFrame({
"PersonID": [1, 2, 3],
"Name": ["Alice", "Bob", "Charlie"]
})
df_right = pd.DataFrame({
"PersonID": [1, 2, 4],
"City": ["NY", "LA", "Houston"]
})
merged_df = pd.merge(df_left, df_right, on="PersonID", how="inner")
print(merged_df)
9.2. Joins on Different Column Names¶
# If columns in the two DataFrames have different names:
pd.merge(df_left, df_right, left_on="PersonID", right_on="ID")
10. Exercises¶
Create a DataFrame from a dictionary of lists with at least three columns.
Load a CSV file into a DataFrame and inspect its first few rows.
Filter rows where a numeric column exceeds a certain threshold.
Perform a group-by operation and calculate the mean of another column.
Merge two DataFrames on a common key.
# 1. Create a DataFrame from a dictionary of lists.
# 2. Load a CSV file and inspect its first few rows.
# 3. Filter rows where a numeric column exceeds a threshold.
# 4. Perform a group-by operation and calculate the mean of another column.
# 5. Merge two DataFrames on a common key.
Run in Google Colab
View on GitHub