How to Analyze Data with Pandas: Essential Techniques
Pandas is a Python library that makes data analysis fast and intuitive. Its DataFrame structure lets you load, clean, transform, and summarize tabular data with minimal code. This tutorial covers the essential steps to start analyzing datasets like a pro.
First, install pandas with pip install pandas and import it as import pandas as pd. Load data using pd.read_csv('file.csv') or pd.read_excel('file.xlsx'). Then immediately inspect the DataFrame to understand its size, columns, and content.
1. Inspect Data with Quick Commands
Use these core methods to get a feel for your dataset:
df.head(n)shows the first n rowsdf.info()displays column types and missing valuesdf.describe()returns summary statistics for numeric columns
2. Handle Missing Data
Real-world datasets often have gaps. Here’s how to deal with them:
df.isnull().sum()counts missing values per columndf.dropna()removes rows with missing valuesdf.fillna(value)replaces missing entries with a specific value
3. Filter and Slice Rows and Columns
Narrow your data to the rows and columns you care about:
df[df['age'] > 30]filters rows where condition is Truedf.loc[df['age'] > 30, ['name','age']]selects specific rows and columnsdf.iloc[1:5, 0:3]slices by integer position
4. Group and Aggregate Data
Summarize values across categories in just a few lines:
df.groupby('city').mean()averages numeric columns by groupdf.groupby('city').agg({'sales':'sum'})applies custom aggregationpd.pivot_table(df, values='sales', index='city')builds a pivot table
Mastering these pandas techniques will let you explore, clean, and summarize data efficiently. Start with a sample dataset and practice each step—soon you’ll be deriving insights from data with confidence.