BUSI2001 Chap.2 Python Fundamentals for Data Analysis
Python Fundamentals for Data Analysis
Python turns business formulas into reusable steps. This chapter covers the workshop's foundations on Google Colab: data types and explicit conversions, operators for profit and return on investment, if and elif logic, for and while loops for growth projections, and functions that package a rule.
It then follows the extract, transform and load sequence on an airline file, encodes categories with label, binary and one-hot methods, and reads correlation and simple regression. The chapter's worked example fits a line to five weeks of advertising and sales by hand and explains why a significant p-value is neither proof of cause nor proof that an effect is large enough to matter to the business.
By the end, Python output should read as a set of claims you can check line by line rather than a block of code you hope is right.
What this chapter covers
- 01
Data types and conversion
- 02
Operators and Boolean tests
- 03
Loops for projections
- 04
Functions and modules
- 05
Extract, transform, load
- 06
Label and one-hot encoding
- 07
Correlation bands
- 08
Regression slope and p-value
Worked example · free
Fitting a sales line to five weeks of advertising spend
- +1Find the means: x averages 3 and y averages 17.
- +1Sum the cross-products of deviations to get 26 and the squared x deviations to get 10.
- +1Slope b = 26 / 10 = 2.6 and intercept a = 17 - 2.6 x 3 = 9.2.
- +1With squared y deviations of 70, r = 26 / the square root of 700 = 0.98, a strong positive relationship.
- +1Forecast at x = 6: 9.2 + 2.6 x 6 = 24.8 thousand dollars, just beyond the observed range.
Key terms
- Data type
- The category Python assigns to a value, such as string, integer, float or Boolean, which decides how it behaves.
- Label encoding
- Replacing ordered categories with integers that preserve their ranking, such as 0 to 3 for Low to Critical.
- Regression slope
- The average change in the outcome for a one-unit increase in the input variable.
- Confounder
- A third variable that influences both variables in a relationship and can create a misleading association.
- Google Colab
- A free cloud-based notebook environment for writing and running Python code in a browser.
- Augmented operator
- A shorthand such as += that updates a variable using its current value in one step.
Python Fundamentals for Data Analysis FAQ
Why does int(0.75) return zero?
Converting a float to an integer in Python drops the decimal part rather than rounding, so 0.75 becomes 0 and 2.9 becomes 2. This matters when rates are stored as fractions. Use round() when rounding is intended, and check the type of any suspicious value before it reaches a calculation.
When should I use one-hot rather than label encoding?
Use label encoding only when the categories have a natural order, such as Low, Medium, High and Critical priority. For nominal categories such as product sub-categories, one-hot encoding creates a separate 0 or 1 column for each level, so a model cannot read a false ranking or equal spacing into the codes.
Does a p-value below 0.05 mean the effect is important?
No. It means the relationship is statistically significant at that level, not that it is large, useful or causal. The course warns that significance often morphs into importance in people's minds. Judge importance by the size of the slope in business units and by context, and look for confounders before claiming cause.
Why does a while loop sometimes never stop?
A while loop repeats until its condition becomes false, so if nothing inside the loop moves the value towards that point, it runs forever. In the revenue-doubling exercise, forgetting to multiply revenue by the growth rate inside the loop would keep the condition true. Always update the tested variable on every pass.
Assessment move
Work in a Colab notebook alongside this chapter. Type each basic exercise yourself before asking an AI assistant for help, so you can tell when its code differs from your intent: assign the four company variables, convert the three values, compute profit and ROI, and write the if-elif discount rule with thresholds in descending order.
Run the five-year investment loop and the doubling loop, printing inside each iteration so you see the values change, and confirm the five-year result with the compound formula. For ETL, prompt for each stage separately and check the output after each one: the data types after extraction, the converted distance after transformation, and the row count after filtering.
For encoding, write down which columns are ordinal and which are nominal before choosing a method. Finally, compute one small regression by hand, as in the worked example, so that software output becomes something you can read critically rather than accept. Keep a personal cheat list of the three conversions, the two loop types and the encoding rules, written in your own words with one example each.
Revisit the regression page after the machine learning topic, because logistic regression extends the same idea of weighting inputs to predict an outcome.
Working through Python Fundamentals for Data Analysis in BUSI2001? Sia is AskSia’s AI Business Analytics tutor — ask any BUSI2001 Python Fundamentals for Data Analysis question and get a clear, step-by-step explanation grounded in how BUSI2001 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.