Easy Python scenarios for everyday data tasks
Scenario 1: Data Cleaning
Question:
You have a DataFrame containing product prices with columns Product and Price. Some of the prices are stored as strings with a dollar sign, like $10. Write a Python function to convert the prices to float.
Answer:
import pandas as pd
data = {
'Product': ['A', 'B', 'C', 'D'],
'Price': ['$10', '$20', '$30', '$40']
}
df = pd.DataFrame(data)
def clean_prices(df):
df['Price'] = df['Price'].str.replace('$', '').astype(float)
return df
cleaned_df = clean_prices(df)
print(cleaned_df)
Scenario 2: Basic Aggregation
Question:
You have a DataFrame containing sales data with columns Region and Sales. Write a Python function to calculate the total sales for each region.
Answer:
import pandas as pd
data = {
'Region': ['North', 'South', 'East', 'West', 'North', 'South', 'East', 'West'],
'Sales': [100, 200, 150, 250, 300, 100, 200, 150]
}
df = pd.DataFrame(data)
def total_sales_per_region(df):
total_sales = df.groupby('Region')['Sales'].sum().reset_index()
return total_sales
total_sales = total_sales_per_region(df)
print(total_sales)
Scenario 3: Filtering Data
Question:
You have a DataFrame containing customer data with columns βCustomerIDβ, Name, and Age. Write a Python function to filter out customers who are younger than 18 years old.
Answer:
import pandas as pd
data = {
'CustomerID': [1, 2, 3, 4, 5],
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [17, 22, 15, 35, 40]
}
df = pd.DataFrame(data)
def filter_customers(df):
filtered_df = df[df['Age'] >= 18]
return filtered_df
filtered_customers = filter_customers(df)
print(filtered_customers)
Scenario 1: Data Cleaning
Question:
You have a DataFrame containing product prices with columns Product and Price. Some of the prices are stored as strings with a dollar sign, like $10. Write a Python function to convert the prices to float.
Answer:
import pandas as pd
data = {
'Product': ['A', 'B', 'C', 'D'],
'Price': ['$10', '$20', '$30', '$40']
}
df = pd.DataFrame(data)
def clean_prices(df):
df['Price'] = df['Price'].str.replace('$', '').astype(float)
return df
cleaned_df = clean_prices(df)
print(cleaned_df)
Scenario 2: Basic Aggregation
Question:
You have a DataFrame containing sales data with columns Region and Sales. Write a Python function to calculate the total sales for each region.
Answer:
import pandas as pd
data = {
'Region': ['North', 'South', 'East', 'West', 'North', 'South', 'East', 'West'],
'Sales': [100, 200, 150, 250, 300, 100, 200, 150]
}
df = pd.DataFrame(data)
def total_sales_per_region(df):
total_sales = df.groupby('Region')['Sales'].sum().reset_index()
return total_sales
total_sales = total_sales_per_region(df)
print(total_sales)
Scenario 3: Filtering Data
Question:
You have a DataFrame containing customer data with columns βCustomerIDβ, Name, and Age. Write a Python function to filter out customers who are younger than 18 years old.
Answer:
import pandas as pd
data = {
'CustomerID': [1, 2, 3, 4, 5],
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [17, 22, 15, 35, 40]
}
df = pd.DataFrame(data)
def filter_customers(df):
filtered_df = df[df['Age'] >= 18]
return filtered_df
filtered_customers = filter_customers(df)
print(filtered_customers)
π25β€8
5 essential Python functions for handling missing data:
πΉ isna(): Detects missing values in your DataFrame. Identifies NaNs
πΉ notna(): Detects non-missing values. Filters out the NaNs.
πΉ interpolate(): Fills missing values using interpolation
πΉ bfill(): Backward fill. Fills missing values with the next valid observation.
πΉ ffill(): Forward fill. Fills missing values with the previous valid observation.
πΉ isna(): Detects missing values in your DataFrame. Identifies NaNs
πΉ notna(): Detects non-missing values. Filters out the NaNs.
πΉ interpolate(): Fills missing values using interpolation
πΉ bfill(): Backward fill. Fills missing values with the next valid observation.
πΉ ffill(): Forward fill. Fills missing values with the previous valid observation.
π14β€10
7 level of writing Python Dictionary
Level 1: Basic Dictionary Creation
Level 2: Accessing and Modifying values
Level 3: Adding and Removing key Values Pairs
Level 4: Dictionary Methods
Level 5: Dictionary Comprehensions
Level 6: Nested Dictionary
Level 7: Advanced Dictionary Operations
I have curated the best interview resources to crack Python Interviews ππ
https://topmate.io/coding/898340
Hope you'll like it
Like this post if you need more resources like this πβ€οΈ
Level 1: Basic Dictionary Creation
Level 2: Accessing and Modifying values
Level 3: Adding and Removing key Values Pairs
Level 4: Dictionary Methods
Level 5: Dictionary Comprehensions
Level 6: Nested Dictionary
Level 7: Advanced Dictionary Operations
I have curated the best interview resources to crack Python Interviews ππ
https://topmate.io/coding/898340
Hope you'll like it
Like this post if you need more resources like this πβ€οΈ
π7β€2
ππ²ππ‘π¨π§ ππ§πππ«π―π’ππ° ππ«ππ©:
Must practise the following questions for your next Python interview:
1. How would you handle missing values in a dataset?
2. Write a python code to merge datasets based on a common column.
3. How would you analyse the distribution of a continuous variable in dataset?
4. Write a python code to pivot an dataframe.
5. How would you handle categorical variables with many levels?
6. Write a python code to calculate the accuracy, precision, and recall of a classification model?
7. How would you handle errors when working with large datasets?
I have curated the best interview resources to crack Python Interviews ππ
https://topmate.io/coding/898340
Hope you'll like it
Like this post if you need more resources like this πβ€οΈ
Must practise the following questions for your next Python interview:
1. How would you handle missing values in a dataset?
2. Write a python code to merge datasets based on a common column.
3. How would you analyse the distribution of a continuous variable in dataset?
4. Write a python code to pivot an dataframe.
5. How would you handle categorical variables with many levels?
6. Write a python code to calculate the accuracy, precision, and recall of a classification model?
7. How would you handle errors when working with large datasets?
I have curated the best interview resources to crack Python Interviews ππ
https://topmate.io/coding/898340
Hope you'll like it
Like this post if you need more resources like this πβ€οΈ
π7β€6
Forwarded from Free Courses with Certificate - Python Programming, Data Science, Java Coding, SQL, Web Development, AI, ML, ChatGPT Expert
Many people reached out to me saying telegram may get banned in their countries. So I've decided to create WhatsApp channels based on your interests ππ
Free Courses with Certificate: https://whatsapp.com/channel/0029Vamhzk5JENy1Zg9KmO2g
Jobs & Internship Opportunities:
https://whatsapp.com/channel/0029VaI5CV93AzNUiZ5Tt226
Web Development: https://whatsapp.com/channel/0029VaiSdWu4NVis9yNEE72z
Python Free Books & Projects: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L
Java Resources: https://whatsapp.com/channel/0029VamdH5mHAdNMHMSBwg1s
Coding Interviews: https://whatsapp.com/channel/0029VammZijATRSlLxywEC3X
SQL: https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Power BI: https://whatsapp.com/channel/0029Vai1xKf1dAvuk6s1v22c
Programming Free Resources: https://whatsapp.com/channel/0029VahiFZQ4o7qN54LTzB17
Data Science Projects: https://whatsapp.com/channel/0029Va4QUHa6rsQjhITHK82y
Learn Data Science & Machine Learning: https://whatsapp.com/channel/0029Va8v3eo1NCrQfGMseL2D
Donβt worry Guys your contact number will stay hidden!
ENJOY LEARNING ππ
Free Courses with Certificate: https://whatsapp.com/channel/0029Vamhzk5JENy1Zg9KmO2g
Jobs & Internship Opportunities:
https://whatsapp.com/channel/0029VaI5CV93AzNUiZ5Tt226
Web Development: https://whatsapp.com/channel/0029VaiSdWu4NVis9yNEE72z
Python Free Books & Projects: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L
Java Resources: https://whatsapp.com/channel/0029VamdH5mHAdNMHMSBwg1s
Coding Interviews: https://whatsapp.com/channel/0029VammZijATRSlLxywEC3X
SQL: https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Power BI: https://whatsapp.com/channel/0029Vai1xKf1dAvuk6s1v22c
Programming Free Resources: https://whatsapp.com/channel/0029VahiFZQ4o7qN54LTzB17
Data Science Projects: https://whatsapp.com/channel/0029Va4QUHa6rsQjhITHK82y
Learn Data Science & Machine Learning: https://whatsapp.com/channel/0029Va8v3eo1NCrQfGMseL2D
Donβt worry Guys your contact number will stay hidden!
ENJOY LEARNING ππ
π7π4β€1
How to Use Pythonβs range() Function
The range() function generates a sequence of numbers, commonly used for looping a specific number of times or creating numeric lists.
The first number is included, but the last number is excluded.
For example, range(5, 10) will generate numbers from 5 to 9, but not 10.
The range() function generates a sequence of numbers, commonly used for looping a specific number of times or creating numeric lists.
The first number is included, but the last number is excluded.
For example, range(5, 10) will generate numbers from 5 to 9, but not 10.
π9
Creating Beautiful Box Plots with Seaborn in Python
A box plot is a simple way to visualise the distribution of a dataset and identify potential outliers. It displays the minimum, first quartile (Q1), median (Q2), third quartile (Q3), and maximum of the data, as well as any outliers. For more details on box plots you can watch my latest video on Insta
πΉ Step 1: Import Seaborn and load your dataset
πΉ Step 2: Create a basic box plot
A box plot is a simple way to visualise the distribution of a dataset and identify potential outliers. It displays the minimum, first quartile (Q1), median (Q2), third quartile (Q3), and maximum of the data, as well as any outliers. For more details on box plots you can watch my latest video on Insta
πΉ Step 1: Import Seaborn and load your dataset
πΉ Step 2: Create a basic box plot
π9π4β€2
Starting your career with Python is an excellent choice due to its versatility and broad range of applications. As you advance, you might discover various specializations that align with your interests:
β’ Data Science: If youβre excited about analyzing data and extracting insights, diving deeper into data science might be your next step. Youβll use Python libraries like Pandas, NumPy, and SciPy to work with data and build predictive models.
β’ Machine Learning: If youβre fascinated by building intelligent systems that learn from data, specializing in machine learning could be your calling. Python frameworks like TensorFlow, Keras, and scikit-learn will be key tools in your toolkit.
β’ Web Development: If you enjoy creating web applications, focusing on web development with Python could be a great path. Frameworks like Django and Flask allow you to build robust and scalable web solutions.
β’ Automation and Scripting: If youβre interested in automating repetitive tasks and creating scripts to improve efficiency, Python is a perfect choice. You'll use libraries like Selenium and BeautifulSoup for web scraping, and automation tools like Celery for task scheduling.
β’ Data Engineering: If youβre keen on building data pipelines and managing large datasets, specializing in data engineering might be your next move. Pythonβs integration with tools like Apache Airflow and Apache Spark can be particularly useful.
β’ DevOps: If you enjoy managing and automating the deployment of applications, focusing on DevOps with Python might be a good fit. Python can be used for scripting and integrating with tools like Docker and Kubernetes.
β’ Game Development: If you're interested in creating games, you might explore game development with Python using libraries like Pygame, which can be a fun and creative way to apply your programming skills.
Even if you stick with general Python programming, thereβs always something new to explore, especially with the constant evolution of libraries and tools.
The key is to continue coding, experimenting with different projects, and staying updated with industry trends. Each step in Python opens up new opportunities to build diverse and impactful applications.
β’ Data Science: If youβre excited about analyzing data and extracting insights, diving deeper into data science might be your next step. Youβll use Python libraries like Pandas, NumPy, and SciPy to work with data and build predictive models.
β’ Machine Learning: If youβre fascinated by building intelligent systems that learn from data, specializing in machine learning could be your calling. Python frameworks like TensorFlow, Keras, and scikit-learn will be key tools in your toolkit.
β’ Web Development: If you enjoy creating web applications, focusing on web development with Python could be a great path. Frameworks like Django and Flask allow you to build robust and scalable web solutions.
β’ Automation and Scripting: If youβre interested in automating repetitive tasks and creating scripts to improve efficiency, Python is a perfect choice. You'll use libraries like Selenium and BeautifulSoup for web scraping, and automation tools like Celery for task scheduling.
β’ Data Engineering: If youβre keen on building data pipelines and managing large datasets, specializing in data engineering might be your next move. Pythonβs integration with tools like Apache Airflow and Apache Spark can be particularly useful.
β’ DevOps: If you enjoy managing and automating the deployment of applications, focusing on DevOps with Python might be a good fit. Python can be used for scripting and integrating with tools like Docker and Kubernetes.
β’ Game Development: If you're interested in creating games, you might explore game development with Python using libraries like Pygame, which can be a fun and creative way to apply your programming skills.
Even if you stick with general Python programming, thereβs always something new to explore, especially with the constant evolution of libraries and tools.
The key is to continue coding, experimenting with different projects, and staying updated with industry trends. Each step in Python opens up new opportunities to build diverse and impactful applications.
π11β€2