<jupyter_start><jupyter_text>Solutions to Lesson 5 ExercisesFor each exercise, the solutions below show one possible way of solving it, but you might have used a different approach, and that's great! There is almost always more than one way to solve any particular problem in Python. Initial Setup Since this notebook is in the `solutions` sub-folder, use the  magic command `%cd` to go up one folder to the main project folder to keep the file paths the same as in the lessons:<jupyter_code>%cd ..<jupyter_output>C:\Users\jenfl\Projects\eoas-python<jupyter_text>Import libraries, display plots inline, initialize variables from lesson:<jupyter_code>import pandas
%matplotlib inline
world = pandas.read_csv('data/gapminder_world_data_2018.csv')
world['area'] = world['population'] / world['pop_density']
world_new = world.set_index('country')
world_new.head(10)<jupyter_output><empty_output><jupyter_text>Exercise 5.1 a) Use `iloc` to select the cell in row 5, column 8 of `world_new`.<jupyter_code>world_new.iloc[5, 8]<jupyter_output><empty_output><jupyter_text>b) Same as a) but using `loc`.<jupyter_code>world_new.loc['Argentina', 'pop_density']<jupyter_output><empty_output><jupyter_text>c) Use `loc` to select the cell in `world_new` containing the population of Australia.<jupyter_code>world_new.loc['Australia', 'population']<jupyter_output><empty_output><jupyter_text>d) Same as c) but using `iloc`<jupyter_code>world_new.iloc[7, 0]<jupyter_output><empty_output><jupyter_text>Exercise 5.2a) Use `iloc` to select the rows 150 up to and including 160, and the first 3 columns of `world_new`.<jupyter_code>world_new.iloc[150:161, :3]<jupyter_output><empty_output><jupyter_text>b) Use `iloc` to select every tenth row (starting from 0) and columns 2, 0, and 3 of world_new.<jupyter_code>world_new.iloc[::10, [2, 0, 3]]<jupyter_output><empty_output><jupyter_text>Exercise 5.3a) Use `loc` to select the GDP per capita and population density in Canada, United States, and Mexico.<jupyter_code>countries = ['Canada', 'United States', 'Mexico']
world_new.loc[countries, ['gdp_per_capita', 'pop_density']]<jupyter_output><empty_output><jupyter_text>b) Use `loc` to select rows where the GDP per capita is greater than `50000`, and a column slice from `'life_expectancy'` through `'children_per_woman'` (inclusive).<jupyter_code>rich = world_new['gdp_per_capita'] > 50000
world_new.loc[rich, 'life_expectancy' : 'children_per_woman']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11300813008130081, 'fraction_numerical': 0.02113821138211382, 'mean_word_length': 7.789285714285715, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '218267', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 895, 'n_tokens_neox': 839, 'n_words': 238, 'path': 'solutions/solutions-5.ipynb', 'repo_name': 'jenfly/eoas-python', 'size': '2460'}