Data Analysis using
Pandas in Python
Introduction to Computer Application
Data Manipulation Missing Values Data Type Conversion
Basic Plotting Statistical Visualizations Interactive Charts
20 Pages · 6 Sections · Ready-to-Use Code Snippets
Table of Contents
1. Working with Pandas 3
• Creating DataFrames 3
• Basic DataFrame Operations 3
• Selecting Data 3
• Filtering 3
• Adding/Removing Columns & Rows 3–4
• Sorting 4
• Grouping and Aggregation 4
• Merging and Joining 4
• Reshaping 4
• String & Datetime Operations 4
• Applying Functions & Exporting 4–5
2. Missing Values 6
• Detecting Missing Values 6
• Removing Missing Values 6
• Filling Missing Values 6
• Replacing Values 6
3. Data Type Conversion 7
• Converting Data Types 7
• DateTime Conversions 7
• String to Numeric 7
• Category Type 7
• Check Data Types 7
4. Matplotlib – Basic Plotting 8
• Basic Line Plot 8
• Scatter Plot 8
• Bar Plot & Histogram 9
• Pie Chart & Subplots 10
• Customization 11
5. Seaborn – Statistical Visualizations 12
• Distribution Plots 12
12–1
• Categorical Plots 3
• Relational Plots 14
• Matrix & Regression Plots 15
• Pair Plots & Joint Plots 16
• Facet Grids & Customization 17
6. Plotly – Interactive Visualizations 18
• Plotly Express (High-level) 18
• Scatter, Bar, Histogram 19
19–2
• Box, Violin, Pie, Heatmap 0
• Common Workflows 22
All code examples are self-contained and ready to run · Python 3.x compatible
df = [Link](['col1', 'col2'], axis=1) # Drop multiple columns
[Link]('column', axis=1, inplace=True) # Drop in place
Adding/Removing Rows
df = [Link](0, axis=0) # Drop row by index
df = [Link]([0, 1, 2], axis=0) # Drop multiple rows
new_row = [Link]([{'A': 1, 'B': 2}])
df = [Link]([df, new_row], ignore_index=True) # Add row
Sorting
df.sort_values('column') # Sort by column (ascending)
df.sort_values('column', ascending=False) # Sort descending
df.sort_values(['col1', 'col2']) # Sort by multiple columns
df.sort_index() # Sort by index
Grouping and Aggregation
[Link]('column').mean() # Group by and calculate mean
[Link]('column').sum() # Group by and sum
[Link]('column').count() # Group by and count
[Link]('column')['col2'].mean() # Group and aggregate specific column
[Link](['col1', 'col2']).mean() # Group by multiple columns
[Link]('column').agg(['mean', 'sum', 'count']) # Multiple aggregations
[Link]('column').agg({'col1': 'mean', 'col2': 'sum'}) # Different agg per column
Merging and Joining
[Link](df1, df2, on='key') # Inner join
[Link](df1, df2, on='key', how='left') # Left join
[Link](df1, df2, on='key', how='right') # Right join
[Link](df1, df2, on='key', how='outer') # Outer join
[Link]([df1, df2], axis=0) # Concatenate vertically
[Link]([df1, df2], axis=1) # Concatenate horizontally
[Link](df2, how='inner') # Join on index
Reshaping
[Link](index='col1', columns='col2', values='col3') # Pivot table
df.pivot_table(values='col3', index='col1', columns='col2', aggfunc='mean')
[Link](id_vars=['col1'], value_vars=['col2', 'col3']) # Unpivot
String Operations
df['column'].[Link]() # Convert to lowercase
df['column'].[Link]() # Convert to uppercase
df['column'].[Link]() # Remove whitespace
df['column'].[Link]('old', 'new') # Replace text
df['column'].[Link](',') # Split string
df['column'].[Link]() # String length
df['column'].[Link]('text') # Check if starts with
df['column'].[Link]('text') # Check if ends with
Datetime Operations
df['date'] = pd.to_datetime(df['date']) # Convert to datetime
df['year'] = df['date'].[Link] # Extract year
df['month'] = df['date'].[Link] # Extract month
df['day'] = df['date'].[Link] # Extract day
df['weekday'] = df['date'].dt.day_name() # Day name
Applying Functions
df['column'].apply(lambda x: x * 2) # Apply function to column
[Link](lambda row: row['A'] + row['B'], axis=1) # Apply to rows
[Link](lambda x: x * 2) # Apply to all elements
Page 2 of 20
Exporting Data
df.to_csv('[Link]', index=False) # Export to CSV
df.to_excel('[Link]', index=False) # Export to Excel
df.to_json('[Link]') # Export to JSON
df.to_html('[Link]') # Export to HTML
Page 3 of 20
Bar Plot
categories = ['A', 'B', 'C', 'D']
values = [4, 7, 1, 8]
[Link](categories, values, color='green', width=0.5)
[Link]('Categories')
[Link]('Values')
[Link]('Bar Plot')
[Link]()
Horizontal bar plot
[Link](categories, values)
[Link]()
Histogram
[Link](data, bins=20, color='blue', edgecolor='black', alpha=0.7)
[Link]('Value')
[Link]('Frequency')
[Link]('Histogram')
Page 7 of 20
[Link]()
Pie Chart
sizes = [15, 30, 45, 10]
labels = ['A', 'B', 'C', 'D']
[Link](sizes, labels=labels, autopct='%1.1f%%', startangle=90)
[Link]('Pie Chart')
[Link]()
Subplots
fig, axes = [Link](2, 2, figsize=(10, 8))
axes[0, 0].plot(x, y)
axes[0, 1].scatter(x, y)
axes[1, 0].bar(categories, values)
axes[1, 1].hist(data)
plt.tight_layout()
[Link]()
Page 8 of 20
Customization
[Link](figsize=(10, 6)) # Set figure size
[Link](0, 10) # Set x-axis limits
[Link](0, 100) # Set y-axis limits
[Link](rotation=45) # Rotate x-axis labels
[Link](True, alpha=0.3) # Add grid
[Link](loc='upper right') # Add legend
[Link]('[Link]', dpi=300, bbox_inches='tight') # Save figure
Page 9 of 20
Page 11 of 20
Count plot
[Link](data=df, x='category', hue='subcategory')
[Link]()
Box plot
[Link](data=df, x='category', y='value')
[Link]()
Violin plot
[Link](data=df, x='category', y='value')
[Link]()
Swarm plot
[Link](data=df, x='category', y='value')
[Link]()
Relational Plots
Scatter plot
[Link](data=df, x='col1', y='col2', hue='category', size='col3')
[Link]()
Page 12 of 20
Line plot
[Link](data=df, x='col1', y='col2', hue='category')
[Link]()
Matrix Plots
Heatmap
[Link]([Link](), annot=True, cmap='coolwarm', center=0)
[Link]()
Clustermap
[Link](df, cmap='viridis', standard_scale=1)
[Link]()
Regression Plots
Scatter with regression line
[Link](data=df, x='col1', y='col2')
[Link]()
Page 13 of 20
Linear model plot with categorical
[Link](data=df, x='col1', y='col2', hue='category')
[Link]()
Pair Plots
Pairwise relationships
[Link](df, hue='category')
[Link]()
Joint Plots
Scatter with distributions
[Link](data=df, x='col1', y='col2', kind='scatter')
[Link]()
Page 14 of 20
Options for kind: scatter, kde, hist, hex, reg, resid
Facet Grids
Multiple subplots
g = [Link](df, col='category', row='subcategory')
[Link]([Link], 'value')
[Link]()
Customization
sns.set_context('talk') # Options: paper, notebook, talk, poster
sns.set_palette('deep') # Color palette
[Link]() # Remove top and right spines
Custom palette
custom_palette = ['#FF6B6B', '#4ECDC4', '#45B7D1']
sns.set_palette(custom_palette)
Page 15 of 20
Page 17 of 20
Page 18 of 20
Scatter plot
fig = [Link](df, x='col1', y='col2', color='category', size='col3',
hover_data=['col4'], title='Scatter Plot')
[Link]()
Bar plot
fig = [Link](df, x='category', y='value', color='subcategory', title='Bar Plot')
[Link]()
Histogram
fig = [Link](df, x='value', nbins=20, title='Histogram')
[Link]()
Box plot
fig = [Link](df, x='category', y='value', color='category')
[Link]()
Violin plot
fig = [Link](df, x='category', y='value', box=True, points='all')
[Link]()
Pie chart
fig = [Link](df, values='value', names='category', title='Pie Chart')
[Link]()
Page 19 of 20
Heatmap
corr_matrix = df[['col1', 'col2', 'col3', 'value']].corr()
fig = [Link](corr_matrix, text_auto=True, color_continuous_scale='RdBu_r')
[Link]()
COMMON WORKFLOWS
Complete Data Analysis Workflow
"""
1. Load Data
df = pd.read_csv('[Link]')
2. Explore Data
[Link]()
[Link]()
[Link]()
[Link]().sum()
3. Clean Data
[Link](inplace=True)
df['column'] = df['column'].astype(int)
df = df[df['value'] > 0]
4. Transform Data
df['new_col'] = df['col1'] + df['col2']
df_grouped = [Link]('category').mean()
5. Visualize
[Link](df['column'])
[Link]()
6. Export
df.to_csv('cleaned_data.csv', index=False)
"""
Error-Safe File Reading
"""
try:
df = pd.read_csv('[Link]')
except FileNotFoundError:
print("File not found!")
except [Link]:
print("File is empty!")
except Exception as e:
print(f"Error: {e}")
"""
Page 20 of 20