Learn PANDAS with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
1
Pandas Simple DataFrame Example
import pandas as pd
# Create DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
# Access column and perform operation
print(df['Name'])
print(df['Age'].mean())
# Filter rows
adults = df[df['Age'] >= 30]
print(adults)
A minimal Pandas example creating a DataFrame, performing simple operations, and printing results.
2
Pandas Read CSV Example
import pandas as pd
# Read CSV file
df = pd.read_csv('data.csv')
# Preview first 5 rows
print(df.head())
Reads a CSV file into a Pandas DataFrame and prints the first few rows.
3
Pandas GroupBy Example
import pandas as pd
# Sample DataFrame
data = {'Department': ['HR','IT','HR','IT'], 'Salary':[50000,60000,55000,65000]}
df = pd.DataFrame(data)
# Group by department and get mean salary
grouped = df.groupby('Department')['Salary'].mean()
print(grouped)
Groups a DataFrame by a column and calculates aggregate statistics.
4
Pandas Merge Example
import pandas as pd
# Sample DataFrames
df1 = pd.DataFrame({'ID':[1,2,3], 'Name':['Alice','Bob','Charlie']})
df2 = pd.DataFrame({'ID':[2,3,4], 'Age':[30,35,40]})
# Merge on ID
merged = pd.merge(df1, df2, on='ID', how='inner')
print(merged)
Merges two DataFrames on a common column.
5
Pandas Pivot Table Example
import pandas as pd
# Sample DataFrame
data = {'Date':['2025-01-01','2025-01-01','2025-01-02'], 'Category':['A','B','A'], 'Value':[10,20,30]}
df = pd.DataFrame(data)
# Pivot table
pivot = df.pivot_table(index='Date', columns='Category', values='Value', aggfunc='sum')
print(pivot)
Creates a pivot table to summarize data.
6
Pandas Drop Columns and Rows Example
import pandas as pd
# Sample DataFrame
data = {'A':[1,2,3], 'B':[4,5,6], 'C':[7,8,9]}
df = pd.DataFrame(data)
# Drop column B
df = df.drop('B', axis=1)
# Drop first row
df = df.drop(0, axis=0)
print(df)
Drops specified columns and rows from a DataFrame.
7
Pandas Fill Missing Values Example
import pandas as pd
import numpy as np
# Sample DataFrame
data = {'A':[1,np.nan,3], 'B':[4,5,np.nan]}
df = pd.DataFrame(data)
# Fill NaN with 0
df = df.fillna(0)
print(df)
Fills NaN values in a DataFrame with a specific value.
8
Pandas Apply Function Example
import pandas as pd
# Sample DataFrame
data = {'Name':['Alice','Bob','Charlie'], 'Age':[25,30,35]}
df = pd.DataFrame(data)
# Apply function to Age column
df['AgeCategory'] = df['Age'].apply(lambda x: 'Adult' if x>=30 else 'Young')
print(df)
Applies a custom function to a DataFrame column.
9
Pandas Sort Values Example
import pandas as pd
# Sample DataFrame
data = {'Name':['Alice','Bob','Charlie'], 'Age':[25,30,35]}
df = pd.DataFrame(data)
# Sort by Age descending
df_sorted = df.sort_values(by='Age', ascending=False)
print(df_sorted)
Sorts a DataFrame by one or more columns.
10
Pandas Boolean Indexing Example
import pandas as pd
# Sample DataFrame
data = {'Name':['Alice','Bob','Charlie'], 'Age':[25,30,35]}
df = pd.DataFrame(data)
# Filter rows where Age > 25
filtered = df[df['Age']>25]
print(filtered)
Filters DataFrame rows using a boolean condition.