📖 Scenario: Imagine you work as a data analyst for a small online store. You have sales data that shows the number of items sold each day. The data is very uneven, with some days having very high sales and many days having low sales. This makes it hard to analyze the data properly.To make the data easier to understand, you will use a log transformation. This helps to reduce the effect of very large numbers and makes the data more balanced.
🎯 Goal: You will create a list of daily sales numbers, then apply a log transformation to reduce skewness. Finally, you will print the transformed data to see the effect.
📋 What You'll Learn
Create a list called daily_sales with exact values: 5, 10, 50, 200, 1000
Create a variable called log_sales to store the log-transformed sales
Use the math.log function to apply the natural log transformation to each sale
Print the log_sales list to show the transformed data
💡 Why This Matters
🌍 Real World
Log transformation is commonly used in data science to handle skewed data, making it easier to analyze and visualize.
💼 Career
Data analysts and scientists use log transformations to prepare data for modeling and to improve the accuracy of statistical analyses.
Progress0 / 4 steps
1
Create the daily sales data
Create a list called daily_sales with these exact values: 5, 10, 50, 200, 1000.
Data Analysis Python
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Import math module for log function
Import the math module so you can use the math.log function for the log transformation.
Data Analysis Python
Hint
Use import math at the top of your code.
3
Apply log transformation to daily sales
Create a list called log_sales that contains the natural log of each value in daily_sales. Use a list comprehension with the variable sale and the math.log function.
Data Analysis Python
Hint
Use a list comprehension: [math.log(sale) for sale in daily_sales].
4
Print the log-transformed sales data
Print the log_sales list to display the log-transformed sales numbers.
Data Analysis Python
Hint
Use print(log_sales) to show the transformed data.
Practice
(1/5)
1. What is the main purpose of applying a log transformation to skewed data?
easy
A. To reduce skewness and make data more normal
B. To increase the range of data values
C. To convert categorical data into numbers
D. To remove missing values from the dataset
Solution
Step 1: Understand skewness in data
Skewed data means the distribution is not symmetric and can affect analysis.
Step 2: Purpose of log transformation
Log transformation reduces skewness by compressing large values and spreading small values, making data more normal.
Final Answer:
To reduce skewness and make data more normal -> Option A
Quick Check:
Log transformation reduces skewness = D [OK]
Hint: Log transformation makes skewed data more normal [OK]
Common Mistakes:
Thinking log increases data range
Confusing log transform with encoding
Assuming it removes missing data
2. Which of the following is the correct Python syntax to apply a natural log transformation to a pandas DataFrame column named 'income'?
easy
A. df['income_log'] = log(df['income'])
B. df['income_log'] = np.log10(df['income'])
C. df['income_log'] = df.log('income')
D. df['income_log'] = np.log(df['income'])
Solution
Step 1: Identify the correct log function
Natural log in Python is computed using np.log() from the numpy library.
Step 2: Apply log to DataFrame column
Use df['income_log'] = np.log(df['income']) to create a new column with log values.
Final Answer:
df['income_log'] = np.log(df['income']) -> Option D
Quick Check:
Use np.log for natural log in pandas = B [OK]
Hint: Use np.log() for natural log on DataFrame columns [OK]
Common Mistakes:
Using log() without np prefix
Trying df.log() method which doesn't exist
Using log10 instead of natural log
3. Given the following code, what will be the output of print(df['log_value'])?
import pandas as pd
import numpy as np
data = {'value': [1, 10, 100, 1000]}
df = pd.DataFrame(data)
df['log_value'] = np.log(df['value'])
print(df['log_value'])
medium
A. [1, 10, 100, 1000]
B. [0.0, 2.302585, 4.605170, 6.907755]
C. [0, 1, 2, 3]
D. Error because log cannot be applied to integers
Solution
Step 1: Understand np.log on positive numbers
Natural log of 1 is 0, log(10) ~ 2.302585, log(100) ~ 4.605170, log(1000) ~ 6.907755.
Step 2: Check output of print statement
Printing the 'log_value' column shows these calculated log values as floats.
Final Answer:
[0.0, 2.302585, 4.605170, 6.907755] -> Option B
Quick Check:
np.log applied to values = A [OK]
Hint: Log of 1 is 0; log grows slowly for larger values [OK]
Common Mistakes:
Expecting original values instead of logs
Confusing log base 10 with natural log
Thinking log can't apply to integers
4. The following code throws an error. What is the most likely cause?
A. Logarithm cannot be applied to zero or negative numbers
B. np.log() requires integer inputs only
C. Missing import statement for pandas
D. Syntax error in the code
Solution
Step 1: Identify input values for log
Values include -5 and 0, which are invalid for logarithm since log is undefined for zero or negative numbers.
Step 2: Understand error cause
Applying np.log to these values causes a runtime warning or error due to invalid input domain.
Final Answer:
Logarithm cannot be applied to zero or negative numbers -> Option A
Quick Check:
Log undefined for ≤ 0 values = C [OK]
Hint: Log only works on positive numbers, not zero or negatives [OK]
Common Mistakes:
Thinking np.log needs integers only
Confusing missing imports as cause
Assuming syntax error without checking inputs
5. You have a dataset with a column 'sales' that contains positive values but also some zeros. You want to apply a log transformation to reduce skewness. Which approach is best to avoid errors and keep data meaningful?
hard
A. Remove all zero values and then apply log: df = df[df['sales'] > 0]; df['log_sales'] = np.log(df['sales'])
B. Replace zeros with -1 before log: df['log_sales'] = np.log(df['sales'].replace(0, -1))
C. Add 1 to all values before log: df['log_sales'] = np.log(df['sales'] + 1)
D. Apply log directly without changes: df['log_sales'] = np.log(df['sales'])
Solution
Step 1: Understand log domain restrictions
Log cannot be applied to zero or negative values, so zeros cause errors.
Step 2: Choose a method to handle zeros
Adding 1 shifts all values positive, allowing log transform without error and preserving data.
Step 3: Evaluate other options
Replacing zeros with -1 causes errors; removing zeros loses data; applying log directly errors on zeros.
Final Answer:
Add 1 to all values before log: df['log_sales'] = np.log(df['sales'] + 1) -> Option C
Quick Check:
Add 1 to avoid log(0) errors = A [OK]
Hint: Add 1 to data before log to handle zeros safely [OK]
Common Mistakes:
Replacing zeros with negative numbers
Dropping zeros losing important data
Applying log directly on zeros
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.