Python for Data Analysis: Quickstart with Pandas & NumPy
Get started with Python data analysis using Pandas and NumPy. Learn to load, clean, analyze, and visualize datasets in this hands-on tutorial.
Prerequisites
- โข Basic Python programming knowledge (variables, loops, functions)
- โข Python 3.8+ installed on your computer
- โข A code editor (VS Code recommended)
Setting Up Your Python Data Analysis Environment
Install Anaconda distribution from anaconda.com โ it bundles Python, Jupyter Notebook, Pandas, NumPy, and Matplotlib together. Alternatively, install packages individually with pip.
Launch Jupyter Notebook: Open terminal/command prompt โ type 'jupyter notebook' โ A browser window opens. Create a new Python 3 notebook. Jupyter lets you write and run code in cells interactively.
Install required libraries if not using Anaconda: 'pip install pandas numpy matplotlib seaborn jupyter'. Verify installation with 'import pandas as pd; print(pd.__version__)'.
Download a practice dataset: Use the Titanic dataset from Kaggle, or any CSV file. We'll use: df = pd.read_csv('data.csv') to load it into a Pandas DataFrame.
Loading and Exploring Data with Pandas
Load your CSV: import pandas as pd; df = pd.read_csv('sales_data.csv'). Pandas can also read Excel (.xlsx), JSON, SQL databases, and even web HTML tables.
First exploration commands: df.head() (first 5 rows), df.shape (rows, columns), df.info() (data types & nulls), df.describe() (statistics for numeric columns).
Check for missing values: df.isnull().sum() shows null counts per column. Visualize with: import seaborn as sns; sns.heatmap(df.isnull(), cbar=True) for a visual null map.
Select specific columns: df['column_name'] or df[['col1', 'col2']]. Filter rows: df[df['Sales'] > 1000]. Combine filters: df[(df['Region'] == 'North') & (df['Sales'] > 500)].
Data Cleaning and Transformation
Handle missing values: df.dropna() removes rows with nulls, df.fillna(0) replaces with zero, df['col'].fillna(df['col'].mean()) fills with column average โ choose based on context.
Change data types: df['Date'] = pd.to_datetime(df['Date']) converts strings to dates. df['Price'] = df['Price'].astype(float) converts to numeric. Wrong types cause calculation errors.
Create new columns: df['Profit'] = df['Revenue'] - df['Cost']. Apply functions: df['Category'] = df['Product'].apply(lambda x: 'Premium' if x.startswith('Pro') else 'Standard').
Group and aggregate: df.groupby('Region')['Sales'].sum() gives total sales by region. Use .agg({'Sales': 'sum', 'Quantity': 'mean'}) for multiple aggregations.
NumPy for Numerical Computing
NumPy is the foundation of data analysis in Python. Import: import numpy as np. Create arrays: arr = np.array([1, 2, 3, 4, 5]). NumPy arrays are 50x faster than Python lists for math.
Array operations are element-wise: arr * 2 doubles every element, arr ** 2 squares each, np.sqrt(arr) takes square roots. No loops needed โ this is called vectorization.
Statistical functions: np.mean(arr), np.median(arr), np.std(arr), np.percentile(arr, 75). These work directly on Pandas columns too: df['Sales'].values gives a NumPy array.
Reshape and manipulate: np.reshape(arr, (2, 3)) changes dimensions, np.concatenate joins arrays, np.where(arr > 3, 'High', 'Low') creates conditional arrays (like Excel IF).
Data Visualization with Matplotlib & Seaborn
Basic plot: import matplotlib.pyplot as plt; plt.plot(df['Date'], df['Sales']); plt.xlabel('Date'); plt.ylabel('Sales'); plt.title('Sales Trend'); plt.show(). Always label your axes.
Bar charts: plt.bar(df['Category'], df['Revenue']). Pie charts: plt.pie(values, labels=labels, autopct='%1.1f%%'). Histograms: plt.hist(df['Age'], bins=20) for distributions.
Seaborn makes beautiful statistical plots: sns.boxplot(x='Region', y='Sales', data=df) for comparing distributions, sns.heatmap(df.corr(), annot=True) for correlation matrices.
Save your plots: plt.savefig('chart.png', dpi=300, bbox_inches='tight'). For reports, use fig, axes = plt.subplots(2, 2, figsize=(12, 8)) to create multi-panel dashboards.
Ready to Go Deeper?
This tutorial covers the basics. Join our instructor-led program for hands-on projects, certification prep, and placement assistance.