Week 2 Tuesday Discussion
Contents
Week 2 Tuesday Discussion¶
Reminders:
Quiz #1 during the last 20 minutes of discussion today
Homework #1 due tonight by 23:59
Today
Any questions about Homework #1?
Review for Quiz #1
Quiz #1 Review¶
Question 1 ¶
Suppose I have the DataFrame shown below. How could I find the sub-DataFrame where the values in the column “Hello” are greater than 30?
import pandas as pd
df = pd.DataFrame({"Hi": [8,45,14,19,40,3,47,37,39,9],"Hello": [44,48,45,12,9,41,28,4,37,38]})
df
Hi | Hello | |
---|---|---|
0 | 8 | 44 |
1 | 45 | 48 |
2 | 14 | 45 |
3 | 19 | 12 |
4 | 40 | 9 |
5 | 3 | 41 |
6 | 47 | 28 |
7 | 37 | 4 |
8 | 39 | 37 |
9 | 9 | 38 |
[c for c in df.columns if df.loc[1,c] > 10]
['Hi', 'Hello']
df["Hello"] > 30
0 True
1 True
2 True
3 False
4 False
5 True
6 False
7 False
8 True
9 True
Name: Hello, dtype: bool
df2 = df[df["Hello"] > 30]
df2
Hi | Hello | |
---|---|---|
0 | 8 | 44 |
1 | 45 | 48 |
2 | 14 | 45 |
5 | 3 | 41 |
8 | 39 | 37 |
9 | 9 | 38 |
Question 2 ¶
Briefly describe the difference between iloc
and loc
. Then, using list comprehension, find all the rows in df
where the corresponding value in the column “Hi” is greater than 10 using iloc
and then again using loc
.
iloc
is used for integer-based indexing (starts at 0); you can remember this by thinking that “i” stands for “integer”.
loc
is used for label-based indexing.
iloc
is used for integer-based indexing (starting at 0), while loc
is label-based indexing. The solution to the second half of this question gives an example of how both can work for the same question.
[r for r in df.index if df.loc[r,"Hi"] > 10]
[1, 2, 3, 4, 6, 7, 8]
[r for r in range(len(df)) if df.iloc[r,0] > 10]
[1, 2, 3, 4, 6, 7, 8]
#tuple
df.shape
(10, 2)
df.index
RangeIndex(start=0, stop=10, step=1)
[r for r in df.index if df.loc[r,"Hi"] > 10]
[1, 2, 3, 4, 6, 7, 8]
df.shape[0]
10
[r for r in range(len(df)) if df.iloc[r,0] > 10]
[1, 2, 3, 4, 6, 7, 8]
[r for r in range(len(df)) if df.iloc[r,0] > 10]
[1, 2, 3, 4, 6, 7, 8]
Question 3 ¶
How could you make the following output using a for-loop and f-strings?
for i in range(0,13,3):
print(f"The square of {i} is {i**2}.")
The square of 0 is 0.
The square of 3 is 9.
The square of 6 is 36.
The square of 9 is 81.
The square of 12 is 144.
for i in range(0,13,3):
print(f"The square of {i} is {i**2}.")
The square of 0 is 0.
The square of 3 is 9.
The square of 6 is 36.
The square of 9 is 81.
The square of 12 is 144.