1/139
Looks like no tags are added yet.
What does it mean to iterate?
To go through the values one at a time
When is for used?
When the number of passes is known (one per row, per item, per file)
The rule with for
The collection says when to stop
How does for work?
for <var> in body>: If there’s any values left in the sequence, assign the next value in <sequence> to <var> and execute the body, then loop back again
What is the sequence in a for code?
What you are going through
<var>
A name you invent. Nothing is special about it.
What does the indented block tell Python in loops?
which lines to repeat (four spaces)
range()
Looping a fixed number of times
list(range(5))
starts at 0, stop before 5 (0, 1, 2, 3, 4)
list(range(1,6))
starts at 1, stops before 6 (1, 2, 3, 4, 5)
list(range(0,10,2))
Every second number up to but not including 10 (0, 2, 4, 6, 8)
len(range(1,6))
How many passes occurred (5)
Where do you place print in loops?
Outside of the indent. That way, it only runs once.
What happens if you place print inside of the indent?
It runs every pass
Accumulator
Name that collects results across all passes. You set it up before the loop and change it inside of the loop.
How is if -> elif -> else tested?
Top to bottom
Where does if -> elif -> else stop?
The first line. Everything below is skipped even if also true
In if -> elif -> else loops, you should put what condition first?
The narrowest
True or false: An inside loop asks its question once per value, and only values that pass get acted on
True
With dictionaries and loops, what should you be mindful of?
Differentiating between values and keys (example: for k, v in price.items() keeps keys and values together)
What happens if you run for k in price
You get keys only as an answer
Building a list using loops
list = [] outside of the loop. At the end of the loop mention list.append(r), with r being "for r in data"
Enumerate
Hands you the position with value (ex: sales = 46, 54, 92. For value in enumerate(sales): print(value) gives (0, 46) (1, 54) (2, 92))
While
The number of passes is not known - repeats until something is true
Rule with while
You must change the condition inside, or it never ends
How does while work?
while <condition>: if <condition> evaluates to true, execute <body> and then loop back.
What must you have in while loops?
A false scenario (ex: x = 5, while x > 0, x = x - 1)
"How long until we double?" is a question answered with what loop?
While (we don't know the answer!)
Parts of a while loop
Something set up before (balance), a condition (balance < 200000), a line inside that moves the condition towards false
Break
Stops the loop entirely, right there
When are breaks used?
When you found out what you came for and the rest doesn't matter
Continue
Skips the rest of this pass and moves to the next value. The loop keeps running.
Nested Loops
The inner loop runs all the way through, once for every pass of the outer one
IndentationError with Loops
Body not indented
NameError with Loops
Accumulator was never started (ex: total = total + r with no total = 0 before the loop)
IndexError with Loops
Last pass asks for a position that isn't there
ValueError with loops
Doesn't specify .items() in dictionary loops
Business Analytics Process
Question/Problem, Data Collection, Processing/Cleaning, Analytics Methods, Decision
Business Analytics
Process of transforming data into actionable insights by identifying meaningful trends, making predictions, and other techniques
Levels of Qualitative Data
Nominal and Ordinal
Nominal Data
Names (may compute counts, mode, shares)
Ordinal Data
Names with order (may compute counts, mode, median)
Levels of Quantitative Data
Discrete and Continuous
Discrete Data
Whole Numbers
Continuous Data
Numbers with Decimals
Types of Continuous Data
Interval and Ratio
Interval Data
No true zero
Ratio Data
True Zero
Time-Series Data
Data collected over many time periods
Cross-Sectional Data
Data collected from several entities at the same time
Name the skew: mean < median < mode
Negative/Left Skewed
Name the skew: mean = median = mode
Normal Distribution
Name the skew: mode < median < mean
Positive/Right skewed
What method of central tendency should you quote in positive/negative skews?
Median
What method of central tendency should you quote in normal distribution?
Mean
Methods of dispersion
Range, variance, standard deviation
The larger the spread, the (larger/smaller) the dispersion
larger
What happens when standard deviation = 0?
Every value is identical
df.info()
Gives non-null counts, data types, shape
df.describe()
Gives count, mean, standard deviation, minimum, quantiles, maximum (numeric columns only)
df["amount"].quantile(0.25)
A quarter of the data is below this number
df["amount"].quantile(0.75)
A quarter of the data is above this number
Which descriptive statistics are not resistance to "bad rows"/outliers?
Mean, standard deviation
Which descriptive statistics are resistant to "bad rows"/outliers?
Median, IQR
df["names"].value_counts()
How many of each value there are
df.groupby("names")["amount"].mean()
Mean of amount per name in names
df.kurt()
How much lives out in the tails
df["names"].nunique()
How many distinct values there are
df.iloc[3]
Gives 3rd index row information
Difference between iloc and loc
iloc doesn't survive a sort while loc does
df.iloc[-1]
Gives last row information
df.iloc[3,2]
Gives one cell by two positions (can also use position, column name)
df.sort_values("value")
Sorts by smallest first (ascending=False reverses this)
df.sort_index()
Back to index order
Primary sources
First-hand gathered data (interviews, surveys, experiments, observations)
Secondary Sources
Collected by others (government data sources, journals, public websites)
4 Dimensions of Data Quality
Accuracy, Completeness, Consistency, Usability
Questions to ask about data
Who collected/produced it? When was the data collected? What was their intent? is it a reputable source?
What happens if you have biased data?
Get new data
What happens if you have outliers?
Exclude, include, or investigate
What happens if you have invalid data?
Fix or remove
What happens if you have missing data?
Drop rows/columns or replace (with mean, median)
What happens if you have duplicates?
Remove by .drop_duplicates()
What happens if you have stale data?
Be cautious with results
What happens if you have inconsistency?
Remove or fix by str.strip(), str.title(), converting dates
Data Cleaning Process Steps
Copy original data, look using info, strip symbols, convert types, drop duplicates, handle blanks, look again
df.shape()
Gives rows and columns as a tuple
df.isna().sum()
How many blanks are there?
df.duplicated().sum()
How many duplicates are there?
df['names'].nunique()
How many distinct spellings are there?
list(df['names'].unique())
What are the distinct spellings in the column 'names'?
copy = df.copy()
Creates a copy of the original dataset
df["column"].astype(float/int/str)
Fixes data types
df['pop'] = df['pop'].fillna("value")
Fills values with "value"
df = df.dropna(axis = 0, how = 'all')
Drops a row (to drop a column, change axis to 1)
df[df.duplicated()]
Point out duplicated rows
df["amount"] = pd.to_numeric(df["amount"], errors ="coerce")
Use if there's data issues while you're trying to convert data types (ex: there's a variable named "pending amongst a sea of numbers). Makes the variable nan.
iqr = df["pop"].quantile(0.75) - df["pop].quantile(0.25)
Finding IQR
max = df["pop"].quantile(0.25) - iqr and min = df["pop"].quantile(0.75) + iqr
Finding the maximum and minimum values
(df[df["pop"] < min]) | (df["pop"] > max)])
Finding outliers