Skip to content

Commit

Permalink
update solutions for JupyterLite
Browse files Browse the repository at this point in the history
  • Loading branch information
laisbsc committed Jul 9, 2023
1 parent 47d3bbd commit 21370c6
Show file tree
Hide file tree
Showing 23 changed files with 1,167 additions and 23 deletions.
4 changes: 1 addition & 3 deletions content/notebooks/5. More Pandas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1078,9 +1078,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df['city'].value_counts()"
]
"source": []
},
{
"cell_type": "code",
Expand Down
7 changes: 6 additions & 1 deletion content/solutions/04_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ def f(x):
"""Returns the argument multiplied by 3 and increased by 10."""
return (x * 3) + 10

print(f(4)) # x = 2 is arbitrary. Try with other values.

print('def f(x):')
print('\t"""Returns the argument multiplied by 3 and increased by 10."""')
print('\treturn (x * 3) + 10\n')

print('f(x) = ', f(4)) # x = 4 is arbitrary. Try with other values.
27 changes: 26 additions & 1 deletion content/solutions/05_27.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
import pandas as pd

df_2014 = pd.read_csv("../data/food_training/training_2014.csv", header=1)
df_2015 = pd.read_csv("../data/food_training/training_2015.csv", header=1)
df_2016 = pd.read_csv("../data/food_training/training_2016.csv", header=1)

frames = [df_2014, df_2015, df_2016]
df = pd.concat(frames)

df = df.reset_index()
df.index

cols_to_remove = ["Unnamed: 5", "Unnamed: 6"]
df = df.drop(cols_to_remove, axis=1)

df[["city", "country"]] = df["Location"].str.split(pat=";", expand=True)

df = df.drop("Location", axis=1)

df["city"] = df["city"].str.lower()

df["city"] = df["city"].str.replace(r"/\w*", "", regex=True)

dict_codes = {
"BG": "Bulgaria",
"CZ": "Czech Republic",
Expand All @@ -8,4 +31,6 @@
}

country_in_codes = df["country"].isin(dict_codes.keys())
df.loc[country_in_codes, "country"] = df.loc[country_in_codes, "country"].map(dict_codes)
df.loc[country_in_codes, "country"] = df.loc[country_in_codes, "country"].map(dict_codes)

print('df.loc[country_in_codes, "country"] = df.loc[country_in_codes, "country"].map(dict_codes)')
39 changes: 38 additions & 1 deletion content/solutions/05_28.py
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
df.loc[df["city"] == "unknown", "country"]
import pandas as pd

df_2014 = pd.read_csv("../data/food_training/training_2014.csv", header=1)
df_2015 = pd.read_csv("../data/food_training/training_2015.csv", header=1)
df_2016 = pd.read_csv("../data/food_training/training_2016.csv", header=1)

frames = [df_2014, df_2015, df_2016]
df = pd.concat(frames)

df = df.reset_index()
df.index

cols_to_remove = ["Unnamed: 5", "Unnamed: 6"]
df = df.drop(cols_to_remove, axis=1)

df[["city", "country"]] = df["Location"].str.split(pat=";", expand=True)

df = df.drop("Location", axis=1)

df["city"] = df["city"].str.lower()

df["city"] = df["city"].str.replace(r"/\w*", "", regex=True)

dict_codes = {
"BG": "Bulgaria",
"CZ": "Czech Republic",
"IT": "Italy",
"GR": "Greece",
"SI": "Slovenia",
"UK": "United Kingdom",
}

country_in_codes = df["country"].isin(dict_codes.keys())
df.loc[country_in_codes, "country"] = df.loc[country_in_codes, "country"].map(dict_codes)

print('df.loc[df["city"] == "unknown", "country"]\n')

display(df.loc[df["city"] == "unknown", "country"])
28 changes: 27 additions & 1 deletion content/solutions/05_29.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
import pandas as pd

df_2014 = pd.read_csv("../data/food_training/training_2014.csv", header=1)
df_2015 = pd.read_csv("../data/food_training/training_2015.csv", header=1)
df_2016 = pd.read_csv("../data/food_training/training_2016.csv", header=1)

frames = [df_2014, df_2015, df_2016]
df = pd.concat(frames)

df = df.reset_index()
df.index

cols_to_remove = ["Unnamed: 5", "Unnamed: 6"]
df = df.drop(cols_to_remove, axis=1)

df[["city", "country"]] = df["Location"].str.split(pat=";", expand=True)

df = df.drop("Location", axis=1)

df["city"] = df["city"].str.lower()

df["city"] = df["city"].str.replace(r"/\w*", "", regex=True)

dict_capitals = {
"Denmark": "copenhague",
"France": "paris",
Expand All @@ -7,4 +30,7 @@
}

unknown_city = df["city"] == "unknown"
df.loc[unknown_city, "city"] = df.loc[unknown_city, "country"].map(dict_capitals)
df.loc[unknown_city, "city"] = df.loc[unknown_city, "country"].map(dict_capitals)

print('unknown_city = df["city"] == "unknown"')
print('df.loc[unknown_city, "city"] = df.loc[unknown_city, "country"].map(dict_capitals)')
40 changes: 39 additions & 1 deletion content/solutions/05_30.py
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
set(df["city"]) - dict_cities.keys()
import pandas as pd

df_2014 = pd.read_csv("../data/food_training/training_2014.csv", header=1)
df_2015 = pd.read_csv("../data/food_training/training_2015.csv", header=1)
df_2016 = pd.read_csv("../data/food_training/training_2016.csv", header=1)

frames = [df_2014, df_2015, df_2016]
df = pd.concat(frames)

df = df.reset_index()
df.index

cols_to_remove = ["Unnamed: 5", "Unnamed: 6"]
df = df.drop(cols_to_remove, axis=1)

df[["city", "country"]] = df["Location"].str.split(pat=";", expand=True)

df = df.drop("Location", axis=1)

df["city"] = df["city"].str.lower()

df["city"] = df["city"].str.replace(r"/\w*", "", regex=True)

dict_capitals = {
"Denmark": "copenhague",
"France": "paris",
"Italy": "rome",
"Spain": "madrid",
"United Kingdom": "london",
}

unknown_city = df["city"] == "unknown"
df.loc[unknown_city, "city"] = df.loc[unknown_city, "country"].map(dict_capitals)

dict_cities = df.loc[df['country'].notnull(), ['city', 'country']].set_index('city').to_dict()['country']

print('set(df["city"]) - dict_cities.keys()\n')

display(set(df["city"]) - dict_cities.keys())
40 changes: 39 additions & 1 deletion content/solutions/05_31.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
import pandas as pd

df_2014 = pd.read_csv("../data/food_training/training_2014.csv", header=1)
df_2015 = pd.read_csv("../data/food_training/training_2015.csv", header=1)
df_2016 = pd.read_csv("../data/food_training/training_2016.csv", header=1)

frames = [df_2014, df_2015, df_2016]
df = pd.concat(frames)

df = df.reset_index()
df.index

cols_to_remove = ["Unnamed: 5", "Unnamed: 6"]
df = df.drop(cols_to_remove, axis=1)

df[["city", "country"]] = df["Location"].str.split(pat=";", expand=True)

df = df.drop("Location", axis=1)

df["city"] = df["city"].str.lower()

df["city"] = df["city"].str.replace(r"/\w*", "", regex=True)

dict_capitals = {
"Denmark": "copenhague",
"France": "paris",
"Italy": "rome",
"Spain": "madrid",
"United Kingdom": "london",
}

unknown_city = df["city"] == "unknown"
df.loc[unknown_city, "city"] = df.loc[unknown_city, "country"].map(dict_capitals)

dict_cities = df.loc[df['country'].notnull(), ['city', 'country']].set_index('city').to_dict()['country']

dict_cities.update(
{
"bristol": "United Kingdom",
Expand All @@ -7,4 +43,6 @@
"murcia": "Spain",
"parma": "Italy",
},
)
)

print("dict_cities.update(\n{\n\t\"bristol\": \"United Kingdom\",\n\t\"gothenburg\": \"Sweden\",\n\t\"graz\": \"Austria\",\n\t\"lyon\": \"France\",\n\t \"murcia\": \"Spain\",\n\t \"parma\": \"Italy\",\n},\n)\n")
52 changes: 51 additions & 1 deletion content/solutions/05_32.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,52 @@
import pandas as pd

df_2014 = pd.read_csv("../data/food_training/training_2014.csv", header=1)
df_2015 = pd.read_csv("../data/food_training/training_2015.csv", header=1)
df_2016 = pd.read_csv("../data/food_training/training_2016.csv", header=1)

frames = [df_2014, df_2015, df_2016]
df = pd.concat(frames)

df = df.reset_index()
df.index

cols_to_remove = ["Unnamed: 5", "Unnamed: 6"]
df = df.drop(cols_to_remove, axis=1)

df[["city", "country"]] = df["Location"].str.split(pat=";", expand=True)

df = df.drop("Location", axis=1)

df["city"] = df["city"].str.lower()

df["city"] = df["city"].str.replace(r"/\w*", "", regex=True)

dict_capitals = {
"Denmark": "copenhague",
"France": "paris",
"Italy": "rome",
"Spain": "madrid",
"United Kingdom": "london",
}

unknown_city = df["city"] == "unknown"
df.loc[unknown_city, "city"] = df.loc[unknown_city, "country"].map(dict_capitals)

dict_cities = df.loc[df['country'].notnull(), ['city', 'country']].set_index('city').to_dict()['country']

dict_cities.update(
{
"bristol": "United Kingdom",
"gothenburg": "Sweden",
"graz": "Austria",
"lyon": "France",
"murcia": "Spain",
"parma": "Italy",
},
)

null_country = df["country"].isnull()
df.loc[null_country, "country"] = df.loc[null_country, "city"].map(dict_cities)
df.loc[null_country, "country"] = df.loc[null_country, "city"].map(dict_cities)

print('null_country = df["country"].isnull()')
print('df.loc[null_country, "country"] = df.loc[null_country, "city"].map(dict_cities)')
54 changes: 53 additions & 1 deletion content/solutions/05_33.py
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
df["country"].value_counts(dropna=False)
import pandas as pd

df_2014 = pd.read_csv("../data/food_training/training_2014.csv", header=1)
df_2015 = pd.read_csv("../data/food_training/training_2015.csv", header=1)
df_2016 = pd.read_csv("../data/food_training/training_2016.csv", header=1)

frames = [df_2014, df_2015, df_2016]
df = pd.concat(frames)

df = df.reset_index()
df.index

cols_to_remove = ["Unnamed: 5", "Unnamed: 6"]
df = df.drop(cols_to_remove, axis=1)

df[["city", "country"]] = df["Location"].str.split(pat=";", expand=True)

df = df.drop("Location", axis=1)

df["city"] = df["city"].str.lower()

df["city"] = df["city"].str.replace(r"/\w*", "", regex=True)

dict_capitals = {
"Denmark": "copenhague",
"France": "paris",
"Italy": "rome",
"Spain": "madrid",
"United Kingdom": "london",
}

unknown_city = df["city"] == "unknown"
df.loc[unknown_city, "city"] = df.loc[unknown_city, "country"].map(dict_capitals)

dict_cities = df.loc[df['country'].notnull(), ['city', 'country']].set_index('city').to_dict()['country']

dict_cities.update(
{
"bristol": "United Kingdom",
"gothenburg": "Sweden",
"graz": "Austria",
"lyon": "France",
"murcia": "Spain",
"parma": "Italy",
},
)

null_country = df["country"].isnull()
df.loc[null_country, "country"] = df.loc[null_country, "city"].map(dict_cities)

print('df["country"].value_counts(dropna=False)\n')

display(df["country"].value_counts(dropna=False))
10 changes: 9 additions & 1 deletion content/solutions/05_34.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ def f(x):
if x == 1:
return "single"
else:
return "multiple"
return "multiple"

print('def f(x):')
print('\tif x == 1:')
print('\t\treturn "single"')
print('\telse:')
print('\t\treturn "multiple"\n')

print('f(4) = ',f(4)) #x = 4
Loading

0 comments on commit 21370c6

Please sign in to comment.