Youโ€™re the Expert!

pynfinity

pandas

Pandas

Your complete recipe for mastering Pandas โ€” course modules & quick-reference guide in one place.

DA Recipe Module 3 sections

BASICS

Create a Pandas DataFrame and Display it

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24],
    'Channel': ['TVS', 'Educational', 'Channel', 'Pynfinity']
}

df = pd.DataFrame(data)
print(df)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Select a Column from DataFrame

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24]
}

df = pd.DataFrame(data)

# Select a column
print(df['Name'])
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Filter Rows Based on Condition

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24]
}

df = pd.DataFrame(data)

# Filter rows where Age is greater than 24
filtered_df = df[df['Age'] > 24]
print(filtered_df)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Adding New Column to DataFrame

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24]
}

df = pd.DataFrame(data)

# Add a new column
df['Channel'] = ['TVS', 'Educational', 'Channel', 'Pynfinity']
print(df)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Handle Missing Data in Pandas

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd
import numpy as np

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', np.nan],
    'Age': [25, 30, np.nan, 24]
}

df = pd.DataFrame(data)

# Fill missing data
df['Name'].fillna('Unknown', inplace=True)
df['Age'].fillna(df['Age'].mean(), inplace=True)

print(df)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

INTERMEDIATE

Group Data by Column

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24],
    'Channel': ['TVS', 'Educational', 'Channel', 'Pynfinity']
}

df = pd.DataFrame(data)

# Group by Channel and calculate mean age
grouped = df.groupby('Channel')['Age'].mean()
print(grouped)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Sorting Data in DataFrame

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24]
}

df = pd.DataFrame(data)

# Sort by Age in descending order
sorted_df = df.sort_values(by='Age', ascending=False)
print(sorted_df)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Merge Two DataFrames

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data1 = {
    'Name': ['Santosh', 'Kumar', 'Dhruv'],
    'Age': [25, 30, 22]
}
data2 = {
    'Name': ['Santosh', 'Kumar', 'Dhruv'],
    'Channel': ['TVS', 'Educational', 'Channel']
}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Merge the DataFrames on 'Name'
merged_df = pd.merge(df1, df2, on='Name')
print(merged_df)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Apply Functions to Columns

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv'],
    'Age': [25, 30, 22]
}

df = pd.DataFrame(data)

# Apply a function to the Age column
df['Age'] = df['Age'].apply(lambda x: x + 1)
print(df)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

ADVANCED

Pivot Table in Pandas

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24],
    'Channel': ['TVS', 'Educational', 'Channel', 'Pynfinity']
}

df = pd.DataFrame(data)

# Create a pivot table
pivot_table = df.pivot_table(values='Age', index='Channel', aggfunc='mean')
print(pivot_table)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Handle Duplicates in DataFrame

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 22, 24]
}

df = pd.DataFrame(data)

# Remove duplicates based on 'Name'
df_no_duplicates = df.drop_duplicates(subset='Name')
print(df_no_duplicates)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Save DataFrame to CSV

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv'],
    'Age': [25, 30, 22]
}

df = pd.DataFrame(data)

# Save DataFrame to CSV file
df.to_csv('output.csv', index=False)
print("DataFrame saved to CSV.")
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.

Load DataFrame from CSV

Explore the concepts and examples below to master this topic.

Code Example
import pandas as pd

# Load data from a CSV file
df = pd.read_csv('output.csv')
print(df)
Video Walkthrough
Coming soon โ€” in-depth video explanation for this topic.
โšก Quick Reference โ€” Recipe

Use the reference cards below for fast lookup. Perfect for brushing up on syntax while you cook your code. ๐Ÿณ

Create a Pandas DataFrame and Display it
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24],
    'Channel': ['TVS', 'Educational', 'Channel', 'Pynfinity']
}

df = pd.DataFrame(data)
print(df)
Select a Column from DataFrame
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24]
}

df = pd.DataFrame(data)

# Select a column
print(df['Name'])
Filter Rows Based on Condition
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24]
}

df = pd.DataFrame(data)

# Filter rows where Age is greater than 24
filtered_df = df[df['Age'] > 24]
print(filtered_df)
Adding New Column to DataFrame
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24]
}

df = pd.DataFrame(data)

# Add a new column
df['Channel'] = ['TVS', 'Educational', 'Channel', 'Pynfinity']
print(df)
Handle Missing Data in Pandas
import pandas as pd
import numpy as np

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', np.nan],
    'Age': [25, 30, np.nan, 24]
}

df = pd.DataFrame(data)

# Fill missing data
df['Name'].fillna('Unknown', inplace=True)
df['Age'].fillna(df['Age'].mean(), inplace=True)

print(df)

Group Data by Column
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24],
    'Channel': ['TVS', 'Educational', 'Channel', 'Pynfinity']
}

df = pd.DataFrame(data)

# Group by Channel and calculate mean age
grouped = df.groupby('Channel')['Age'].mean()
print(grouped)
Sorting Data in DataFrame
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24]
}

df = pd.DataFrame(data)

# Sort by Age in descending order
sorted_df = df.sort_values(by='Age', ascending=False)
print(sorted_df)
Merge Two DataFrames
import pandas as pd

data1 = {
    'Name': ['Santosh', 'Kumar', 'Dhruv'],
    'Age': [25, 30, 22]
}
data2 = {
    'Name': ['Santosh', 'Kumar', 'Dhruv'],
    'Channel': ['TVS', 'Educational', 'Channel']
}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Merge the DataFrames on 'Name'
merged_df = pd.merge(df1, df2, on='Name')
print(merged_df)
Apply Functions to Columns
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv'],
    'Age': [25, 30, 22]
}

df = pd.DataFrame(data)

# Apply a function to the Age column
df['Age'] = df['Age'].apply(lambda x: x + 1)
print(df)

Pivot Table in Pandas
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 24],
    'Channel': ['TVS', 'Educational', 'Channel', 'Pynfinity']
}

df = pd.DataFrame(data)

# Create a pivot table
pivot_table = df.pivot_table(values='Age', index='Channel', aggfunc='mean')
print(pivot_table)
Handle Duplicates in DataFrame
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv', 'Dhruv', 'Pynfinity'],
    'Age': [25, 30, 22, 22, 24]
}

df = pd.DataFrame(data)

# Remove duplicates based on 'Name'
df_no_duplicates = df.drop_duplicates(subset='Name')
print(df_no_duplicates)
Save DataFrame to CSV
import pandas as pd

data = {
    'Name': ['Santosh', 'Kumar', 'Dhruv'],
    'Age': [25, 30, 22]
}

df = pd.DataFrame(data)

# Save DataFrame to CSV file
df.to_csv('output.csv', index=False)
print("DataFrame saved to CSV.")
Load DataFrame from CSV
import pandas as pd

# Load data from a CSV file
df = pd.read_csv('output.csv')
print(df)

Recipe Complete! ๐ŸŽ‰

You've explored the full Pandas recipe.
Keep practising โ€” a language a day keeps AI away! ๐Ÿค–

Back to DA Courses


Pynfinity
Install Pynfinity Add to home screen for the best experience