BASICS
Create a Pandas DataFrame and Display it
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
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)
INTERMEDIATE
Group Data by Column
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
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)
ADVANCED
Pivot Table in Pandas
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
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
Explore the concepts and examples below to master this topic.
import pandas as pd
# Load data from a CSV file
df = pd.read_csv('output.csv')
print(df)
Use the reference cards below for fast lookup. Perfect for brushing up on syntax while you cook your code. ๐ณ
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)
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'])
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)
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)
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)
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)
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)
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)
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)
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)
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)
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.")
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! ๐ค