def squares_a_list(numerical_list):
new_squared_list = list()
for number in numerical_list:
new_squared_list.append(number ** 2)
return new_squared_listWhat makes a function useful?
Is a function more useful when it does more operations?
Do adding parameters make your functions more or less functional?
These are all questions we need to think about when writing functions.
Hard coding is the process of embedding values directly into your code without saving them in objects.
def load_filter_and_average(file, grouping_column, ploting_column):
df = pd.read_csv("data/" + file)
source = df.groupby(grouping_column).mean(numeric_only=True).reset_index()
chart = alt.Chart(source, width = 500, height = 300).mark_bar().encode(
x=alt.X(grouping_column),
y=alt.Y(ploting_column)
)
return chart| mfr | calories | protein | fat | ... | shelf | weight | cups | rating | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | A | 100.000000 | 4.000000 | 1.000000 | ... | 2.000000 | 1.000000 | 1.000000 | 54.850917 |
| 1 | G | 111.363636 | 2.318182 | 1.363636 | ... | 2.136364 | 1.049091 | 0.875000 | 34.485852 |
| 2 | K | 108.695652 | 2.652174 | 0.608696 | ... | 2.347826 | 1.077826 | 0.796087 | 44.038462 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 4 | P | 108.888889 | 2.444444 | 0.888889 | ... | 2.444444 | 1.064444 | 0.714444 | 41.705744 |
| 5 | Q | 95.000000 | 2.625000 | 1.750000 | ... | 2.375000 | 0.875000 | 0.823750 | 42.915990 |
| 6 | R | 115.000000 | 2.500000 | 1.250000 | ... | 2.000000 | 1.000000 | 0.871250 | 41.542997 |
7 rows × 14 columns
def load_filter_and_average(file, grouping_column, ploting_column):
df = pd.read_csv("data/" + file)
source = df.groupby(grouping_column).mean(numeric_only=True).reset_index()
chart = alt.Chart(source, width = 500, height = 300).mark_bar().encode(
x=alt.X(grouping_column),
y=alt.Y(ploting_column)
)
return chart, source(alt.Chart(...),
mfr calories protein ... weight cups rating
0 A 100.000000 4.000000 ... 1.000000 1.000000 54.850917
1 G 111.363636 2.318182 ... 1.049091 0.875000 34.485852
2 K 108.695652 2.652174 ... 1.077826 0.796087 44.038462
.. .. ... ... ... ... ... ...
4 P 108.888889 2.444444 ... 1.064444 0.714444 41.705744
5 Q 95.000000 2.625000 ... 0.875000 0.823750 42.915990
6 R 115.000000 2.500000 ... 1.000000 0.871250 41.542997
[7 rows x 14 columns])
| mfr | calories | protein | ... | weight | cups | rating | |
|---|---|---|---|---|---|---|---|
| 0 | A | 100.000000 | 4.000000 | ... | 1.000000 | 1.000000 | 54.850917 |
| 1 | G | 111.363636 | 2.318182 | ... | 1.049091 | 0.875000 | 34.485852 |
| 2 | K | 108.695652 | 2.652174 | ... | 1.077826 | 0.796087 | 44.038462 |
3 rows × 14 columns
AttributeError: 'str' object has no attribute 'groupby'
Detailed traceback:
File "<string>", line 1, in <module>
File "<string>", line 2, in bad_grouped_means