4.Easy Python scenarios for everyday data tasks.
Pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool,built on top of the Python programming language.
Scenario 1: Data Cleaning
Question: You have a Data frame 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 Data frame 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 Data frame 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)
For more info visit below link:-
pandas – Python Data Analysis Library (pydata.org)
Teaser-1 (Python If, Elif) – EducateNow Cafe