Learn Pandas - 10 Code Examples & CST Typing Practice Test
Pandas is an open-source Python library that provides high-performance, easy-to-use data structures and data analysis tools for working with structured (tabular, multidimensional, and time-series) data.
View all 10 Pandas code examples →
Learn PANDAS with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Pandas
What is Pandas?
Pandas is an open-source Python library that provides high-performance, easy-to-use data structures and data analysis tools for working with structured (tabular, multidimensional, and time-series) data.
What are the primary use cases for Pandas?
Data cleaning, wrangling, and preprocessing. Exploratory data analysis (EDA) and statistics. Time-series analysis and financial data handling. Merging, joining, and reshaping datasets. Integration with visualization and ML frameworks
What are the strengths of Pandas?
Highly expressive and concise API. Excellent performance on medium-sized datasets. Seamless integration with NumPy and SciPy. Rich ecosystem of data science libraries. Robust support for missing data and time-series analysis
What are the limitations of Pandas?
Not optimized for extremely large datasets (consider Dask or PySpark). High memory usage with very large DataFrames. Single-threaded operations limit parallel processing. Some complex operations require chaining and careful handling. Learning curve for multi-index and advanced groupby operations
How can I practice Pandas typing speed?
CodeSpeedTest offers 10+ real Pandas code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.