"""
EXERCICE 1
---
- print() the last item from both the year and the pop list to see what the predicted population for the year 2100 is. Use two print() functions.
- Before you can start, you should import matplotlib.pyplot as plt. pyplot is a sub-package of matplotlib, hence the dot.
- Use plt.plot() to build a line plot. year should be mapped on the horizontal axis, pop on the vertical axis. Don't forget to finish off with the plt.show() function to actually display the plot.
"""
year = [1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050, 2051, 2052, 2053, 2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2094, 2095, 2096, 2097, 2098, 2099, 2100]
pop = [2.53, 2.57, 2.62, 2.67, 2.71, 2.76, 2.81, 2.86, 2.92, 2.97, 3.03, 3.08, 3.14, 3.2, 3.26, 3.33, 3.4, 3.47, 3.54, 3.62, 3.69, 3.77, 3.84, 3.92, 4.0, 4.07, 4.15, 4.22, 4.3, 4.37, 4.45, 4.53, 4.61, 4.69, 4.78, 4.86, 4.95, 5.05, 5.14, 5.23, 5.32, 5.41, 5.49, 5.58, 5.66, 5.74, 5.82, 5.9, 5.98, 6.05, 6.13, 6.2, 6.28, 6.36, 6.44, 6.51, 6.59, 6.67, 6.75, 6.83, 6.92, 7.0, 7.08, 7.16, 7.24, 7.32, 7.4, 7.48, 7.56, 7.64, 7.72, 7.79, 7.87, 7.94, 8.01, 8.08, 8.15, 8.22, 8.29, 8.36, 8.42, 8.49, 8.56, 8.62, 8.68, 8.74, 8.8, 8.86, 8.92, 8.98, 9.04, 9.09, 9.15, 9.2, 9.26, 9.31, 9.36, 9.41, 9.46, 9.5, 9.55, 9.6, 9.64, 9.68, 9.73, 9.77, 9.81, 9.85, 9.88, 9.92, 9.96, 9.99, 10.03, 10.06, 10.09, 10.13, 10.16, 10.19, 10.22, 10.25, 10.28, 10.31, 10.33, 10.36, 10.38, 10.41, 10.43, 10.46, 10.48, 10.5, 10.52, 10.55, 10.57, 10.59, 10.61, 10.63, 10.65, 10.66, 10.68, 10.7, 10.72, 10.73, 10.75, 10.77, 10.78, 10.79, 10.81, 10.82, 10.83, 10.84, 10.85]
# Print the last item from year and pop
print(year[-1])
print(pop[-1])
# Import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
# Make a line plot: year on the x-axis, pop on the y-axis
plt.plot(year, pop)
plt.show()
2100 10.85
"""
EXERCICE 2
---
- Print the last item from both the list gdp_cap, and the list life_exp; it is information about Zimbabwe.
- Build a line chart, with gdp_cap on the x-axis, and life_exp on the y-axis. Does it make sense to plot this data on a line plot?
- Don't forget to finish off with a plt.show() command, to actually display the plot.
"""
gdp_cap = [974.5803384, 5937.029525999999, 6223.367465, 4797.231267, 12779.37964, 34435.367439999995, 36126.4927, 29796.04834, 1391.253792, 33692.60508, 1441.284873, 3822.137084, 7446.298803, 12569.85177, 9065.800825, 10680.79282, 1217.032994, 430.0706916, 1713.778686, 2042.09524, 36319.23501, 706.016537, 1704.063724, 13171.63885, 4959.114854, 7006.580419, 986.1478792, 277.5518587, 3632.557798, 9645.06142, 1544.750112, 14619.222719999998, 8948.102923, 22833.30851, 35278.41874, 2082.4815670000003, 6025.374752000001, 6873.262326000001, 5581.180998, 5728.353514, 12154.08975, 641.3695236000001, 690.8055759, 33207.0844, 30470.0167, 13206.48452, 752.7497265, 32170.37442, 1327.60891, 27538.41188, 5186.050003, 942.6542111, 579.2317429999999, 1201.637154, 3548.3308460000003, 39724.97867, 18008.94444, 36180.78919, 2452.210407, 3540.651564, 11605.71449, 4471.061906, 40675.99635, 25523.2771, 28569.7197, 7320.880262000001, 31656.06806, 4519.461171, 1463.249282, 1593.06548, 23348.139730000003, 47306.98978, 10461.05868, 1569.331442, 414.5073415, 12057.49928, 1044.770126, 759.3499101, 12451.6558, 1042.581557, 1803.151496, 10956.99112, 11977.57496, 3095.7722710000003, 9253.896111, 3820.17523, 823.6856205, 944.0, 4811.060429, 1091.359778, 36797.93332, 25185.00911, 2749.320965, 619.6768923999999, 2013.977305, 49357.19017, 22316.19287, 2605.94758, 9809.185636, 4172.838464, 7408.905561, 3190.481016, 15389.924680000002, 20509.64777, 19328.70901, 7670.122558, 10808.47561, 863.0884639000001, 1598.435089, 21654.83194, 1712.472136, 9786.534714, 862.5407561000001, 47143.17964, 18678.31435, 25768.25759, 926.1410683, 9269.657808, 28821.0637, 3970.095407, 2602.394995, 4513.480643, 33859.74835, 37506.41907, 4184.548089, 28718.27684, 1107.482182, 7458.396326999999, 882.9699437999999, 18008.50924, 7092.923025, 8458.276384, 1056.380121, 33203.26128, 42951.65309, 10611.46299, 11415.80569, 2441.576404, 3025.349798, 2280.769906, 1271.211593, 469.70929810000007]
life_exp = [43.828, 76.423, 72.301, 42.731, 75.32, 81.235, 79.829, 75.635, 64.062, 79.441, 56.728, 65.554, 74.852, 50.728, 72.39, 73.005, 52.295, 49.58, 59.723, 50.43, 80.653, 44.74100000000001, 50.651, 78.553, 72.961, 72.889, 65.152, 46.462, 55.322, 78.782, 48.328, 75.748, 78.273, 76.486, 78.332, 54.791, 72.235, 74.994, 71.33800000000001, 71.878, 51.57899999999999, 58.04, 52.947, 79.313, 80.657, 56.735, 59.448, 79.406, 60.022, 79.483, 70.259, 56.007, 46.388000000000005, 60.916, 70.19800000000001, 82.208, 73.33800000000001, 81.757, 64.69800000000001, 70.65, 70.964, 59.545, 78.885, 80.745, 80.546, 72.567, 82.603, 72.535, 54.11, 67.297, 78.623, 77.58800000000001, 71.993, 42.592, 45.678, 73.952, 59.443000000000005, 48.303, 74.241, 54.467, 64.164, 72.801, 76.195, 66.803, 74.543, 71.164, 42.082, 62.069, 52.906000000000006, 63.785, 79.762, 80.204, 72.899, 56.867, 46.859, 80.196, 75.64, 65.483, 75.53699999999999, 71.752, 71.421, 71.688, 75.563, 78.098, 78.74600000000001, 76.442, 72.476, 46.242, 65.528, 72.777, 63.062, 74.002, 42.568000000000005, 79.972, 74.663, 77.926, 48.159, 49.339, 80.941, 72.396, 58.556, 39.613, 80.884, 81.70100000000001, 74.143, 78.4, 52.517, 70.616, 58.42, 69.819, 73.923, 71.777, 51.542, 79.425, 78.242, 76.384, 73.747, 74.249, 73.422, 62.698, 42.38399999999999, 43.487]
# Print the last item of gdp_cap and life_exp
print(gdp_cap[-1])
print(life_exp[-1])
# Make a line plot, gdp_cap on the x-axis, life_exp on the y-axis
plt.plot(gdp_cap, life_exp)
# Display the plot
plt.show()
469.70929810000007 43.487
"""
EXERCICE 3
---
- Change the line plot that's coded in the script to a scatter plot.
- A correlation will become clear when you display the GDP per capita on a logarithmic scale. Add the line plt.xscale('log').
- Finish off your script with plt.show() to display the plot.
"""
# Change the line plot below to a scatter plot
plt.scatter(gdp_cap, life_exp)
# Put the x-axis on a logarithmic scale
plt.xscale('log')
# Show plot
plt.show()
"""
EXERCICE 4
---
- Build a scatter plot, where pop_2007 is mapped on the horizontal axis, and life_exp is mapped on the vertical axis.
- Finish the script with plt.show() to actually display the plot. Do you see a correlation?
"""
pop_2007 = [31.889923, 3.600523, 33.333216, 12.420476, 40.301927, 20.434176, 8.199783, 0.708573, 150.448339, 10.392226, 8.078314, 9.119152, 4.552198, 1.639131, 190.010647, 7.322858, 14.326203, 8.390505, 14.131858, 17.696293, 33.390141, 4.369038, 10.238807, 16.284741, 1318.683096, 44.22755, 0.71096, 64.606759, 3.80061, 4.133884, 18.013409, 4.493312, 11.416987, 10.228744, 5.46812, 0.496374, 9.319622, 13.75568, 80.264543, 6.939688, 0.551201, 4.906585, 76.511887, 5.23846, 61.083916, 1.454867, 1.688359, 82.400996, 22.873338, 10.70629, 12.572928, 9.947814, 1.472041, 8.502814, 7.483763, 6.980412, 9.956108, 0.301931, 1110.396331, 223.547, 69.45357, 27.499638, 4.109086, 6.426679, 58.147733, 2.780132, 127.467972, 6.053193, 35.610177, 23.301725, 49.04479, 2.505559, 3.921278, 2.012649, 3.193942, 6.036914, 19.167654, 13.327079, 24.821286, 12.031795, 3.270065, 1.250882, 108.700891, 2.874127, 0.684736, 33.757175, 19.951656, 47.76198, 2.05508, 28.90179, 16.570613, 4.115771, 5.675356, 12.894865, 135.031164, 4.627926, 3.204897, 169.270617, 3.242173, 6.667147, 28.674757, 91.077287, 38.518241, 10.642836, 3.942491, 0.798094, 22.276056, 8.860588, 0.199579, 27.601038, 12.267493, 10.150265, 6.144562, 4.553009, 5.447502, 2.009245, 9.118773, 43.997828, 40.448191, 20.378239, 42.292929, 1.133066, 9.031088, 7.554661, 19.314747, 23.174294, 38.13964, 65.068149, 5.701579, 1.056608, 10.276158, 71.158647, 29.170398, 60.776238, 301.139947, 3.447496, 26.084662, 85.262356, 4.018332, 22.211743, 11.746035, 12.311143]
# Build Scatter plot
plt.scatter(pop_2007, life_exp)
plt.xscale('log')
# Show plot
plt.show()
# Definition of countries and capital
countries = ['spain', 'france', 'germany', 'norway', 'italy']
capitals = ['madrid', 'paris', 'berlin', 'oslo', 'rome']
# From string in countries and capitals, create dictionary europe
list(zip(countries, capitals))
[('spain', 'madrid'),
('france', 'paris'),
('germany', 'berlin'),
('norway', 'oslo'),
('italy', 'rome')]
zip(countries, capitals)
<zip at 0x7fed5fbe97c0>
dict(zip(countries, capitals))
{'spain': 'madrid', 'france': 'paris', 'germany': 'berlin', 'norway': 'oslo'}
# Definition of dictionary
europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }
# Print out the keys in europe
print(europe.keys())
dict_keys(['spain', 'france', 'germany', 'norway'])
# Print out the keys in europe
print(europe.values())
dict_values(['madrid', 'paris', 'berlin', 'oslo'])
print(europe.items())
dict_items([('spain', 'madrid'), ('france', 'paris'), ('germany', 'berlin'), ('norway', 'oslo')])
# Print out value that belongs to key 'norway'
print(europe['norway'])
europe = {'spain':'madrid', 'france':'paris', 'germany':'bonn',
'norway':'oslo', 'italy':'rome', 'poland':'warsaw',
'australia':'vienna' }
# Update capital of germany
europe['germany'] = 'berlin'
# Print europe
print(europe)
{'spain': 'madrid', 'france': 'paris', 'germany': 'berlin', 'norway': 'oslo', 'italy': 'rome', 'poland': 'warsaw', 'australia': 'vienna'}
europe['new_spain'] = europe['spain']
del europe['spain']
europe
{'france': 'paris',
'germany': 'berlin',
'norway': 'oslo',
'italy': 'rome',
'poland': 'warsaw',
'australia': 'vienna',
'new_spain': 'madrid'}
import pandas as pd
brics = pd.read_csv('./../02-intermediate/brics.csv')
brics
| Unnamed: 0 | country | capital | area | population | |
|---|---|---|---|---|---|
| 0 | BR | Brazil | Brasilia | 8.516 | 200.40 |
| 1 | RU | Russia | Moscow | 17.100 | 143.50 |
| 2 | IN | India | New Delhi | 3.286 | 1252.00 |
| 3 | CH | China | Beijing | 9.597 | 1357.00 |
| 4 | SA | South Africa | Pretoria | 1.221 | 52.98 |
brics = pd.read_csv('./../02-intermediate/brics.csv', index_col=0)
brics
| country | capital | area | population | |
|---|---|---|---|---|
| BR | Brazil | Brasilia | 8.516 | 200.40 |
| RU | Russia | Moscow | 17.100 | 143.50 |
| IN | India | New Delhi | 3.286 | 1252.00 |
| CH | China | Beijing | 9.597 | 1357.00 |
| SA | South Africa | Pretoria | 1.221 | 52.98 |
# Build cars DataFrame
names = ['United States', 'Australia', 'Japan', 'India', 'Russia', 'Morocco', 'Egypt']
dr = [True, False, False, False, True, True, True]
cpc = [809, 731, 588, 18, 200, 70, 45]
dict = { 'country':names, 'drives_right':dr, 'cars_per_cap':cpc }
cars = pd.DataFrame.from_dict(dict)
print(cars)
country drives_right cars_per_cap 0 United States True 809 1 Australia False 731 2 Japan False 588 3 India False 18 4 Russia True 200 5 Morocco True 70 6 Egypt True 45
# Definition of row_labels
row_labels = ['US', 'AUS', 'JAP', 'IN', 'RU', 'MOR', 'EG']
list(cars.index)
[0, 1, 2, 3, 4, 5, 6]
# Specify row labels of cars
cars.index = row_labels
# Print cars again
print(cars)
country drives_right cars_per_cap US United States True 809 AUS Australia False 731 JAP Japan False 588 IN India False 18 RU Russia True 200 MOR Morocco True 70 EG Egypt True 45
# Import the cars.csv data: cars
cars = pd.read_csv('/Users/samiadrappeau/Documents/TBS/PYTHON-101/code/02-intermediate/cars.csv') # ADAPT THE PATH TO YOURS
# Print out cars
print(cars)
Unnamed: 0 cars_per_cap country drives_right 0 US 809 United States True 1 AUS 731 Australia False 2 JPN 588 Japan False 3 IN 18 India False 4 RU 200 Russia True 5 MOR 70 Morocco True 6 EG 45 Egypt True
# Import the cars.csv data: cars
cars = pd.read_csv('/Users/samiadrappeau/Documents/TBS/PYTHON-101/code/02-intermediate/cars.csv', index_col=0) # ADAPT THE PATH TO YOURS
# Print out cars
print(cars)
cars_per_cap country drives_right US 809 United States True AUS 731 Australia False JPN 588 Japan False IN 18 India False RU 200 Russia True MOR 70 Morocco True EG 45 Egypt True
# Import the cars.csv data: cars
cars = pd.read_csv("./, index_col=0) # ADAPT THE PATH TO YOURS
# Print out cars
print(cars)
brics = pd.read_csv('../02-intermediate/brics.csv', index_col=0)
brics
| country | capital | area | population | |
|---|---|---|---|---|
| BR | Brazil | Brasilia | 8.516 | 200.40 |
| RU | Russia | Moscow | 17.100 | 143.50 |
| IN | India | New Delhi | 3.286 | 1252.00 |
| CH | China | Beijing | 9.597 | 1357.00 |
| SA | South Africa | Pretoria | 1.221 | 52.98 |
brics.loc[:, "country"]
BR Brazil RU Russia IN India CH China SA South Africa Name: country, dtype: object
type(brics.loc[:, "country"])
pandas.core.series.Series
brics[:, ["country", "capital"]]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [57], in <cell line: 1>() ----> 1 brics[:, ["country", "capital"]] File ~/anaconda3/envs/datascience/lib/python3.8/site-packages/pandas/core/frame.py:3024, in DataFrame.__getitem__(self, key) 3022 if self.columns.nlevels > 1: 3023 return self._getitem_multilevel(key) -> 3024 indexer = self.columns.get_loc(key) 3025 if is_integer(indexer): 3026 indexer = [indexer] File ~/anaconda3/envs/datascience/lib/python3.8/site-packages/pandas/core/indexes/base.py:3080, in Index.get_loc(self, key, method, tolerance) 3078 casted_key = self._maybe_cast_indexer(key) 3079 try: -> 3080 return self._engine.get_loc(casted_key) 3081 except KeyError as err: 3082 raise KeyError(key) from err File pandas/_libs/index.pyx:70, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/index.pyx:75, in pandas._libs.index.IndexEngine.get_loc() TypeError: '(slice(None, None, None), ['country', 'capital'])' is an invalid key
brics[["country", "capital"]]
| country | capital | |
|---|---|---|
| BR | Brazil | Brasilia |
| RU | Russia | Moscow |
| IN | India | New Delhi |
| CH | China | Beijing |
| SA | South Africa | Pretoria |
brics["country", "capital"]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File ~/anaconda3/envs/datascience/lib/python3.8/site-packages/pandas/core/indexes/base.py:3080, in Index.get_loc(self, key, method, tolerance) 3079 try: -> 3080 return self._engine.get_loc(casted_key) 3081 except KeyError as err: File pandas/_libs/index.pyx:70, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/index.pyx:101, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/hashtable_class_helper.pxi:4554, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas/_libs/hashtable_class_helper.pxi:4562, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: ('country', 'capital') The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Input In [59], in <cell line: 1>() ----> 1 brics["country", "capital"] File ~/anaconda3/envs/datascience/lib/python3.8/site-packages/pandas/core/frame.py:3024, in DataFrame.__getitem__(self, key) 3022 if self.columns.nlevels > 1: 3023 return self._getitem_multilevel(key) -> 3024 indexer = self.columns.get_loc(key) 3025 if is_integer(indexer): 3026 indexer = [indexer] File ~/anaconda3/envs/datascience/lib/python3.8/site-packages/pandas/core/indexes/base.py:3082, in Index.get_loc(self, key, method, tolerance) 3080 return self._engine.get_loc(casted_key) 3081 except KeyError as err: -> 3082 raise KeyError(key) from err 3084 if tolerance is not None: 3085 tolerance = self._convert_tolerance(tolerance, np.asarray(key)) KeyError: ('country', 'capital')
brics
| country | capital | area | population | |
|---|---|---|---|---|
| BR | Brazil | Brasilia | 8.516 | 200.40 |
| RU | Russia | Moscow | 17.100 | 143.50 |
| IN | India | New Delhi | 3.286 | 1252.00 |
| CH | China | Beijing | 9.597 | 1357.00 |
| SA | South Africa | Pretoria | 1.221 | 52.98 |
brics.columns
Index(['country', 'capital', 'area', 'population'], dtype='object')
brics.index
Index([' BR', ' RU', ' IN', ' CH', ' SA'], dtype='object')
"pyscript" == "Pyscript"
False
import numpy as np
my_house = np.array([18.0, 20.0, 10.75, 9.50, 25.0])
your_house = np.array([14.0, 24.0, 14.25, 9.0])
print(len(my_house))
print(len(your_house))
5 4
my_house < your_house
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [67], in <cell line: 1>() ----> 1 my_house < your_house ValueError: operands could not be broadcast together with shapes (5,) (4,)
16 < my_house < 20
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [68], in <cell line: 1>() ----> 1 16 < my_house < 20 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
my_kitchen = 18.0
your_kitchen = 14.0
2* my_kitchen < 3*your_kitchen
True
# Create arrays
import numpy as np
my_house = np.array([18.0, 20.0, 10.75, 9.50])
your_house = np.array([14.0, 24.0, 14.25, 9.0])
# my_house greater than 18.5 or smaller than 10
np.logical_or(my_house > 18.5, my_house < 10)
array([False, True, False, True])
# Both my_house and your_house smaller than 11
my_house < 11 and your_house < 11
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [73], in <cell line: 2>() 1 # Both my_house and your_house smaller than 11 ----> 2 my_house < 11 and your_house < 11 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
np.logical_and(my_house < 11, your_house < 11)
array([False, False, False, True])
"""
EXERCICE 1
---
- Extract the drives_right column as a Pandas Series and store it as dr.
- Use dr, a boolean Series, to subset the cars DataFrame. Store the resulting selection in sel.
- Print sel, and assert that drives_right is True for all observations.
"""
# Import cars data
import pandas as pd
cars = pd.read_csv('/Users/samiadrappeau/Documents/TBS/PYTHON-101/code/02-intermediate/cars.csv', index_col = 0) # ADAPT THE PATH TO YOURS
# Extract drives_right column as Series: dr
dr = cars["drives_right"]
print(dr)
US True AUS False JPN False IN False RU True MOR True EG True Name: drives_right, dtype: bool
# Use dr to subset cars: sel
sel = cars[dr]
# Print sel
print(sel)
cars_per_cap country drives_right US 809 United States True RU 200 Russia True MOR 70 Morocco True EG 45 Egypt True
print(cars[cars["drives_right"]])
cars_per_cap country drives_right US 809 United States True RU 200 Russia True MOR 70 Morocco True EG 45 Egypt True
"""
EXERCICE 3
---
- Select the cars_per_cap column from cars as a Pandas Series and store it as cpc.
- Use cpc in combination with a comparison operator and 500. You want to end up with a boolean Series that's True if the corresponding country has a cars_per_cap of more than 500 and False otherwise. Store this boolean Series as many_cars.
- Use many_cars to subset cars, similar to what you did before. Store the result as car_maniac.
- Print out car_maniac to see if you got it right.
"""
# Select the cars_per_cap column
cpc = cars["cars_per_cap"]
print(cpc)
# Create car_maniac: observations that have a cars_per_cap over 500
car_maniac = cpc > 500
print(car_maniac)
# Print car_maniac
US 809 AUS 731 JPN 588 IN 18 RU 200 MOR 70 EG 45 Name: cars_per_cap, dtype: int64 US True AUS True JPN True IN False RU False MOR False EG False Name: cars_per_cap, dtype: bool
"""
EXERCICE 4
---
- Use the code sample provided to create a DataFrame medium, that includes all the observations of cars that have a cars_per_cap between 100 and 500.
Print out medium.
"""
# Import numpy, you'll need this
import numpy as np
# Create medium: observations with cars_per_cap between 100 and 500
medium = np.logical_and(cars["cars_per_cap"] > 100, cars["cars_per_cap"] < 500)
# Print medium
print(medium)
cars[medium]
US False AUS False JPN False IN False RU True MOR False EG False Name: cars_per_cap, dtype: bool
| cars_per_cap | country | drives_right | |
|---|---|---|---|
| RU | 200 | Russia | True |
cars[(cars["cars_per_cap"] > 100) & (cars["cars_per_cap"] < 500)]
| cars_per_cap | country | drives_right | |
|---|---|---|---|
| RU | 200 | Russia | True |
# Initialize offset
offset = 8
# Code the while loop
while offset != 0:
print("correcting...")
offset = offset -1
print(offset)
correcting... 7 correcting... 6 correcting... 5 correcting... 4 correcting... 3 correcting... 2 correcting... 1 correcting... 0
# Initialize offset
offset = -6
# Code the while loop
while offset != 0 :
print("correcting...")
if offset > 0 :
offset = offset - 1
else :
offset = offset +1
print(offset)
correcting... -5 correcting... -4 correcting... -3 correcting... -2 correcting... -1 correcting... 0
# areas list
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Code the for loop
for x in areas:
print(x)
11.25 18.0 20.0 10.75 9.5
# Code the for loop
for i, x in enumerate(areas):
print(i, ": ", x)
0 : 11.25 1 : 18.0 2 : 20.0 3 : 10.75 4 : 9.5
for x in enumerate(areas):
print(x)
(0, 11.25) (1, 18.0) (2, 20.0) (3, 10.75) (4, 9.5)
for index, area in enumerate(areas) :
print("room " + str(index+1) + ": " + str(area))
room 1: 11.25 room 2: 18.0 room 3: 20.0 room 4: 10.75 room 5: 9.5
house = [["hallway", 11.25],
["kitchen", 18.0],
["living room", 20.0],
["bedroom", 10.75],
["bathroom", 9.50]]
for [x, y] in house:
print(str(x) + " is " + str(y) + " sqm")
print(f"{x} is {y} sqm")
hallway is 11.25 sqm hallway is 11.25 sqm kitchen is 18.0 sqm kitchen is 18.0 sqm living room is 20.0 sqm living room is 20.0 sqm bedroom is 10.75 sqm bedroom is 10.75 sqm bathroom is 9.5 sqm bathroom is 9.5 sqm
for x, y in house:
print(str(x) + " is " + str(y) + " sqm")
print(f"{x} is {y} sqm")
hallway is 11.25 sqm hallway is 11.25 sqm kitchen is 18.0 sqm kitchen is 18.0 sqm living room is 20.0 sqm living room is 20.0 sqm bedroom is 10.75 sqm bedroom is 10.75 sqm bathroom is 9.5 sqm bathroom is 9.5 sqm
# Definition of dictionary
europe = {'spain':'madrid', 'france':'paris', 'germany':'bonn',
'norway':'oslo', 'italy':'rome', 'poland':'warsaw', 'australia':'vienna' }
# Iterate over europe
for x, y in europe.items():
print("the capital of " + x.capitalize() + " is " + y.capitalize())
the capital of Spain is Madrid the capital of France is Paris the capital of Germany is Bonn the capital of Norway is Oslo the capital of Italy is Rome the capital of Poland is Warsaw the capital of Australia is Vienna
"""
EXERCICE 2
---
- Import the numpy package under the local alias np.
- Write a for loop that iterates over all elements in np_height and prints out "x inches" for each element, where x is the value in the array.
- Write a for loop that visits every element of the np_baseball array and prints it out.
"""
# Import numpy as np
import numpy as np
height_in = [74, 74, 72, 72, 73, 69, 69, 71, 76, 71, 73, 73, 74, 74, 69, 70, 73, 75, 78, 79, 76, 74, 76, 72, 71, 75, 77, 74, 73, 74, 78, 73, 75, 73, 75, 75, 74, 69, 71, 74, 73, 73, 76, 74, 74, 70, 72, 77, 74, 70, 73, 75, 76, 76, 78, 74, 74, 76, 77, 81, 78, 75, 77, 75, 76, 74, 72, 72, 75, 73, 73, 73, 70, 70, 70, 76, 68, 71, 72, 75, 75, 75, 75, 68, 74, 78, 71, 73, 76, 74, 74, 79, 75, 73, 76, 74, 74, 73, 72, 74, 73, 74, 72, 73, 69, 72, 73, 75, 75, 73, 72, 72, 76, 74, 72, 77, 74, 77, 75, 76, 80, 74, 74, 75, 78, 73, 73, 74, 75, 76, 71, 73, 74, 76, 76, 74, 73, 74, 70, 72, 73, 73, 73, 73, 71, 74, 74, 72, 74, 71, 74, 73, 75, 75, 79, 73, 75, 76, 74, 76, 78, 74, 76, 72, 74, 76, 74, 75, 78, 75, 72, 74, 72, 74, 70, 71, 70, 75, 71, 71, 73, 72, 71, 73, 72, 75, 74, 74, 75, 73, 77, 73, 76, 75, 74, 76, 75, 73, 71, 76, 75, 72, 71, 77, 73, 74, 71, 72, 74, 75, 73, 72, 75, 75, 74, 72, 74, 71, 70, 74, 77, 77, 75, 75, 78, 75, 76, 73, 75, 75, 79, 77, 76, 71, 75, 74, 69, 71, 76, 72, 72, 70, 72, 73, 71, 72, 71, 73, 72, 73, 74, 74, 72, 75, 74, 74, 77, 75, 73, 72, 71, 74, 77, 75, 75, 75, 78, 78, 74, 76, 78, 76, 70, 72, 80, 74, 74, 71, 70, 72, 71, 74, 71, 72, 71, 74, 69, 76, 75, 75, 76, 73, 76, 73, 77, 73, 72, 72, 77, 77, 71, 74, 74, 73, 78, 75, 73, 70, 74, 72, 73, 73, 75, 75, 74, 76, 73, 74, 75, 75, 72, 73, 73, 72, 74, 78, 76, 73, 74, 75, 70, 75, 71, 72, 78, 75, 73, 73, 71, 75, 77, 72, 69, 73, 74, 72, 70, 75, 70, 72, 72, 74, 73, 74, 76, 75, 80, 72, 75, 73, 74, 74, 73, 75, 75, 71, 73, 75, 74, 74, 72, 74, 74, 74, 73, 76, 75, 72, 73, 73, 73, 72, 72, 72, 72, 71, 75, 75, 74, 73, 75, 79, 74, 76, 73, 74, 74, 72, 74, 74, 75, 78, 74, 74, 74, 77, 70, 73, 74, 73, 71, 75, 71, 72, 77, 74, 70, 77, 73, 72, 76, 71, 76, 78, 75, 73, 78, 74, 79, 75, 76, 72, 75, 75, 70, 72, 70, 74, 71, 76, 73, 76, 71, 69, 72, 72, 69, 73, 69, 73, 74, 74, 72, 71, 72, 72, 76, 76, 76, 74, 76, 75, 71, 72, 71, 73, 75, 76, 75, 71, 75, 74, 72, 73, 73, 73, 73, 76, 72, 76, 73, 73, 73, 75, 75, 77, 73, 72, 75, 70, 74, 72, 80, 71, 71, 74, 74, 73, 75, 76, 73, 77, 72, 73, 77, 76, 71, 75, 73, 74, 77, 71, 72, 73, 69, 73, 70, 74, 76, 73, 73, 75, 73, 79, 74, 73, 74, 77, 75, 74, 73, 77, 73, 77, 74, 74, 73, 77, 74, 77, 75, 77, 75, 71, 74, 70, 79, 72, 72, 70, 74, 74, 72, 73, 72, 74, 74, 76, 82, 74, 74, 70, 73, 73, 74, 77, 72, 76, 73, 73, 72, 74, 74, 71, 72, 75, 74, 74, 77, 70, 71, 73, 76, 71, 75, 74, 72, 76, 79, 76, 73, 76, 78, 75, 76, 72, 72, 73, 73, 75, 71, 76, 70, 75, 74, 75, 73, 71, 71, 72, 73, 73, 72, 69, 73, 78, 71, 73, 75, 76, 70, 74, 77, 75, 79, 72, 77, 73, 75, 75, 75, 73, 73, 76, 77, 75, 70, 71, 71, 75, 74, 69, 70, 75, 72, 75, 73, 72, 72, 72, 76, 75, 74, 69, 73, 72, 72, 75, 77, 76, 80, 77, 76, 79, 71, 75, 73, 76, 77, 73, 76, 70, 75, 73, 75, 70, 69, 71, 72, 72, 73, 70, 70, 73, 76, 75, 72, 73, 79, 71, 72, 74, 74, 74, 72, 76, 76, 72, 72, 71, 72, 72, 70, 77, 74, 72, 76, 71, 76, 71, 73, 70, 73, 73, 72, 71, 71, 71, 72, 72, 74, 74, 74, 71, 72, 75, 72, 71, 72, 72, 72, 72, 74, 74, 77, 75, 73, 75, 73, 76, 72, 77, 75, 72, 71, 71, 75, 72, 73, 73, 71, 70, 75, 71, 76, 73, 68, 71, 72, 74, 77, 72, 76, 78, 81, 72, 73, 76, 72, 72, 74, 76, 73, 76, 75, 70, 71, 74, 72, 73, 76, 76, 73, 71, 68, 71, 71, 74, 77, 69, 72, 76, 75, 76, 75, 76, 72, 74, 76, 74, 72, 75, 78, 77, 70, 72, 79, 74, 71, 68, 77, 75, 71, 72, 70, 72, 72, 73, 72, 74, 72, 72, 75, 72, 73, 74, 72, 78, 75, 72, 74, 75, 75, 76, 74, 74, 73, 74, 71, 74, 75, 76, 74, 76, 76, 73, 75, 75, 74, 68, 72, 75, 71, 70, 72, 73, 72, 75, 74, 70, 76, 71, 82, 72, 73, 74, 71, 75, 77, 72, 74, 72, 73, 78, 77, 73, 73, 73, 73, 73, 76, 75, 70, 73, 72, 73, 75, 74, 73, 73, 76, 73, 75, 70, 77, 72, 77, 74, 75, 75, 75, 75, 72, 74, 71, 76, 71, 75, 76, 83, 75, 74, 76, 72, 72, 75, 75, 72, 77, 73, 72, 70, 74, 72, 74, 72, 71, 70, 71, 76, 74, 76, 74, 74, 74, 75, 75, 71, 71, 74, 77, 71, 74, 75, 77, 76, 74, 76, 72, 71, 72, 75, 73, 68, 72, 69, 73, 73, 75, 70, 70, 74, 75, 74, 74, 73, 74, 75, 77, 73, 74, 76, 74, 75, 73, 76, 78, 75, 73, 77, 74, 72, 74, 72, 71, 73, 75, 73, 67, 67, 76, 74, 73, 70, 75, 70, 72, 77, 79, 78, 74, 75, 75, 78, 76, 75, 69, 75, 72, 75, 73, 74, 75, 75, 73]
np_height = np.array(height_in)
baseball = [[74, 180], [74, 215], [72, 210], [72, 210], [73, 188], [69, 176], [69, 209], [71, 200], [76, 231], [71, 180], [73, 188], [73, 180], [74, 185], [74, 160], [69, 180], [70, 185], [73, 189], [75, 185], [78, 219], [79, 230], [76, 205], [74, 230], [76, 195], [72, 180], [71, 192], [75, 225], [77, 203], [74, 195], [73, 182], [74, 188], [78, 200], [73, 180], [75, 200], [73, 200], [75, 245], [75, 240], [74, 215], [69, 185], [71, 175], [74, 199], [73, 200], [73, 215], [76, 200], [74, 205], [74, 206], [70, 186], [72, 188], [77, 220], [74, 210], [70, 195], [73, 200], [75, 200], [76, 212], [76, 224], [78, 210], [74, 205], [74, 220], [76, 195], [77, 200], [81, 260], [78, 228], [75, 270], [77, 200], [75, 210], [76, 190], [74, 220], [72, 180], [72, 205], [75, 210], [73, 220], [73, 211], [73, 200], [70, 180], [70, 190], [70, 170], [76, 230], [68, 155], [71, 185], [72, 185], [75, 200], [75, 225], [75, 225], [75, 220], [68, 160], [74, 205], [78, 235], [71, 250], [73, 210], [76, 190], [74, 160], [74, 200], [79, 205], [75, 222], [73, 195], [76, 205], [74, 220], [74, 220], [73, 170], [72, 185], [74, 195], [73, 220], [74, 230], [72, 180], [73, 220], [69, 180], [72, 180], [73, 170], [75, 210], [75, 215], [73, 200], [72, 213], [72, 180], [76, 192], [74, 235], [72, 185], [77, 235], [74, 210], [77, 222], [75, 210], [76, 230], [80, 220], [74, 180], [74, 190], [75, 200], [78, 210], [73, 194], [73, 180], [74, 190], [75, 240], [76, 200], [71, 198], [73, 200], [74, 195], [76, 210], [76, 220], [74, 190], [73, 210], [74, 225], [70, 180], [72, 185], [73, 170], [73, 185], [73, 185], [73, 180], [71, 178], [74, 175], [74, 200], [72, 204], [74, 211], [71, 190], [74, 210], [73, 190], [75, 190], [75, 185], [79, 290], [73, 175], [75, 185], [76, 200], [74, 220], [76, 170], [78, 220], [74, 190], [76, 220], [72, 205], [74, 200], [76, 250], [74, 225], [75, 215], [78, 210], [75, 215], [72, 195], [74, 200], [72, 194], [74, 220], [70, 180], [71, 180], [70, 170], [75, 195], [71, 180], [71, 170], [73, 206], [72, 205], [71, 200], [73, 225], [72, 201], [75, 225], [74, 233], [74, 180], [75, 225], [73, 180], [77, 220], [73, 180], [76, 237], [75, 215], [74, 190], [76, 235], [75, 190], [73, 180], [71, 165], [76, 195], [75, 200], [72, 190], [71, 190], [77, 185], [73, 185], [74, 205], [71, 190], [72, 205], [74, 206], [75, 220], [73, 208], [72, 170], [75, 195], [75, 210], [74, 190], [72, 211], [74, 230], [71, 170], [70, 185], [74, 185], [77, 241], [77, 225], [75, 210], [75, 175], [78, 230], [75, 200], [76, 215], [73, 198], [75, 226], [75, 278], [79, 215], [77, 230], [76, 240], [71, 184], [75, 219], [74, 170], [69, 218], [71, 190], [76, 225], [72, 220], [72, 176], [70, 190], [72, 197], [73, 204], [71, 167], [72, 180], [71, 195], [73, 220], [72, 215], [73, 185], [74, 190], [74, 205], [72, 205], [75, 200], [74, 210], [74, 215], [77, 200], [75, 205], [73, 211], [72, 190], [71, 208], [74, 200], [77, 210], [75, 232], [75, 230], [75, 210], [78, 220], [78, 210], [74, 202], [76, 212], [78, 225], [76, 170], [70, 190], [72, 200], [80, 237], [74, 220], [74, 170], [71, 193], [70, 190], [72, 150], [71, 220], [74, 200], [71, 190], [72, 185], [71, 185], [74, 200], [69, 172], [76, 220], [75, 225], [75, 190], [76, 195], [73, 219], [76, 190], [73, 197], [77, 200], [73, 195], [72, 210], [72, 177], [77, 220], [77, 235], [71, 180], [74, 195], [74, 195], [73, 190], [78, 230], [75, 190], [73, 200], [70, 190], [74, 190], [72, 200], [73, 200], [73, 184], [75, 200], [75, 180], [74, 219], [76, 187], [73, 200], [74, 220], [75, 205], [75, 190], [72, 170], [73, 160], [73, 215], [72, 175], [74, 205], [78, 200], [76, 214], [73, 200], [74, 190], [75, 180], [70, 205], [75, 220], [71, 190], [72, 215], [78, 235], [75, 191], [73, 200], [73, 181], [71, 200], [75, 210], [77, 240], [72, 185], [69, 165], [73, 190], [74, 185], [72, 175], [70, 155], [75, 210], [70, 170], [72, 175], [72, 220], [74, 210], [73, 205], [74, 200], [76, 205], [75, 195], [80, 240], [72, 150], [75, 200], [73, 215], [74, 202], [74, 200], [73, 190], [75, 205], [75, 190], [71, 160], [73, 215], [75, 185], [74, 200], [74, 190], [72, 210], [74, 185], [74, 220], [74, 190], [73, 202], [76, 205], [75, 220], [72, 175], [73, 160], [73, 190], [73, 200], [72, 229], [72, 206], [72, 220], [72, 180], [71, 195], [75, 175], [75, 188], [74, 230], [73, 190], [75, 200], [79, 190], [74, 219], [76, 235], [73, 180], [74, 180], [74, 180], [72, 200], [74, 234], [74, 185], [75, 220], [78, 223], [74, 200], [74, 210], [74, 200], [77, 210], [70, 190], [73, 177], [74, 227], [73, 180], [71, 195], [75, 199], [71, 175], [72, 185], [77, 240], [74, 210], [70, 180], [77, 194], [73, 225], [72, 180], [76, 205], [71, 193], [76, 230], [78, 230], [75, 220], [73, 200], [78, 249], [74, 190], [79, 208], [75, 245], [76, 250], [72, 160], [75, 192], [75, 220], [70, 170], [72, 197], [70, 155], [74, 190], [71, 200], [76, 220], [73, 210], [76, 228], [71, 190], [69, 160], [72, 184], [72, 180], [69, 180], [73, 200], [69, 176], [73, 160], [74, 222], [74, 211], [72, 195], [71, 200], [72, 175], [72, 206], [76, 240], [76, 185], [76, 260], [74, 185], [76, 221], [75, 205], [71, 200], [72, 170], [71, 201], [73, 205], [75, 185], [76, 205], [75, 245], [71, 220], [75, 210], [74, 220], [72, 185], [73, 175], [73, 170], [73, 180], [73, 200], [76, 210], [72, 175], [76, 220], [73, 206], [73, 180], [73, 210], [75, 195], [75, 200], [77, 200], [73, 164], [72, 180], [75, 220], [70, 195], [74, 205], [72, 170], [80, 240], [71, 210], [71, 195], [74, 200], [74, 205], [73, 192], [75, 190], [76, 170], [73, 240], [77, 200], [72, 205], [73, 175], [77, 250], [76, 220], [71, 224], [75, 210], [73, 195], [74, 180], [77, 245], [71, 175], [72, 180], [73, 215], [69, 175], [73, 180], [70, 195], [74, 230], [76, 230], [73, 205], [73, 215], [75, 195], [73, 180], [79, 205], [74, 180], [73, 190], [74, 180], [77, 190], [75, 190], [74, 220], [73, 210], [77, 255], [73, 190], [77, 230], [74, 200], [74, 205], [73, 210], [77, 225], [74, 215], [77, 220], [75, 205], [77, 200], [75, 220], [71, 197], [74, 225], [70, 187], [79, 245], [72, 185], [72, 185], [70, 175], [74, 200], [74, 180], [72, 188], [73, 225], [72, 200], [74, 210], [74, 245], [76, 213], [82, 231], [74, 165], [74, 228], [70, 210], [73, 250], [73, 191], [74, 190], [77, 200], [72, 215], [76, 254], [73, 232], [73, 180], [72, 215], [74, 220], [74, 180], [71, 200], [72, 170], [75, 195], [74, 210], [74, 200], [77, 220], [70, 165], [71, 180], [73, 200], [76, 200], [71, 170], [75, 224], [74, 220], [72, 180], [76, 198], [79, 240], [76, 239], [73, 185], [76, 210], [78, 220], [75, 200], [76, 195], [72, 220], [72, 230], [73, 170], [73, 220], [75, 230], [71, 165], [76, 205], [70, 192], [75, 210], [74, 205], [75, 200], [73, 210], [71, 185], [71, 195], [72, 202], [73, 205], [73, 195], [72, 180], [69, 200], [73, 185], [78, 240], [71, 185], [73, 220], [75, 205], [76, 205], [70, 180], [74, 201], [77, 190], [75, 208], [79, 240], [72, 180], [77, 230], [73, 195], [75, 215], [75, 190], [75, 195], [73, 215], [73, 215], [76, 220], [77, 220], [75, 230], [70, 195], [71, 190], [71, 195], [75, 209], [74, 204], [69, 170], [70, 185], [75, 205], [72, 175], [75, 210], [73, 190], [72, 180], [72, 180], [72, 160], [76, 235], [75, 200], [74, 210], [69, 180], [73, 190], [72, 197], [72, 203], [75, 205], [77, 170], [76, 200], [80, 250], [77, 200], [76, 220], [79, 200], [71, 190], [75, 170], [73, 190], [76, 220], [77, 215], [73, 206], [76, 215], [70, 185], [75, 235], [73, 188], [75, 230], [70, 195], [69, 168], [71, 190], [72, 160], [72, 200], [73, 200], [70, 189], [70, 180], [73, 190], [76, 200], [75, 220], [72, 187], [73, 240], [79, 190], [71, 180], [72, 185], [74, 210], [74, 220], [74, 219], [72, 190], [76, 193], [76, 175], [72, 180], [72, 215], [71, 210], [72, 200], [72, 190], [70, 185], [77, 220], [74, 170], [72, 195], [76, 205], [71, 195], [76, 210], [71, 190], [73, 190], [70, 180], [73, 220], [73, 190], [72, 186], [71, 185], [71, 190], [71, 180], [72, 190], [72, 170], [74, 210], [74, 240], [74, 220], [71, 180], [72, 210], [75, 210], [72, 195], [71, 160], [72, 180], [72, 205], [72, 200], [72, 185], [74, 245], [74, 190], [77, 210], [75, 200], [73, 200], [75, 222], [73, 215], [76, 240], [72, 170], [77, 220], [75, 156], [72, 190], [71, 202], [71, 221], [75, 200], [72, 190], [73, 210], [73, 190], [71, 200], [70, 165], [75, 190], [71, 185], [76, 230], [73, 208], [68, 209], [71, 175], [72, 180], [74, 200], [77, 205], [72, 200], [76, 250], [78, 210], [81, 230], [72, 244], [73, 202], [76, 240], [72, 200], [72, 215], [74, 177], [76, 210], [73, 170], [76, 215], [75, 217], [70, 198], [71, 200], [74, 220], [72, 170], [73, 200], [76, 230], [76, 231], [73, 183], [71, 192], [68, 167], [71, 190], [71, 180], [74, 180], [77, 215], [69, 160], [72, 205], [76, 223], [75, 175], [76, 170], [75, 190], [76, 240], [72, 175], [74, 230], [76, 223], [74, 196], [72, 167], [75, 195], [78, 190], [77, 250], [70, 190], [72, 190], [79, 190], [74, 170], [71, 160], [68, 150], [77, 225], [75, 220], [71, 209], [72, 210], [70, 176], [72, 260], [72, 195], [73, 190], [72, 184], [74, 180], [72, 195], [72, 195], [75, 219], [72, 225], [73, 212], [74, 202], [72, 185], [78, 200], [75, 209], [72, 200], [74, 195], [75, 228], [75, 210], [76, 190], [74, 212], [74, 190], [73, 218], [74, 220], [71, 190], [74, 235], [75, 210], [76, 200], [74, 188], [76, 210], [76, 235], [73, 188], [75, 215], [75, 216], [74, 220], [68, 180], [72, 185], [75, 200], [71, 210], [70, 220], [72, 185], [73, 231], [72, 210], [75, 195], [74, 200], [70, 205], [76, 200], [71, 190], [82, 250], [72, 185], [73, 180], [74, 170], [71, 180], [75, 208], [77, 235], [72, 215], [74, 244], [72, 220], [73, 185], [78, 230], [77, 190], [73, 200], [73, 180], [73, 190], [73, 196], [73, 180], [76, 230], [75, 224], [70, 160], [73, 178], [72, 205], [73, 185], [75, 210], [74, 180], [73, 190], [73, 200], [76, 257], [73, 190], [75, 220], [70, 165], [77, 205], [72, 200], [77, 208], [74, 185], [75, 215], [75, 170], [75, 235], [75, 210], [72, 170], [74, 180], [71, 170], [76, 190], [71, 150], [75, 230], [76, 203], [83, 260], [75, 246], [74, 186], [76, 210], [72, 198], [72, 210], [75, 215], [75, 180], [72, 200], [77, 245], [73, 200], [72, 192], [70, 192], [74, 200], [72, 192], [74, 205], [72, 190], [71, 186], [70, 170], [71, 197], [76, 219], [74, 200], [76, 220], [74, 207], [74, 225], [74, 207], [75, 212], [75, 225], [71, 170], [71, 190], [74, 210], [77, 230], [71, 210], [74, 200], [75, 238], [77, 234], [76, 222], [74, 200], [76, 190], [72, 170], [71, 220], [72, 223], [75, 210], [73, 215], [68, 196], [72, 175], [69, 175], [73, 189], [73, 205], [75, 210], [70, 180], [70, 180], [74, 197], [75, 220], [74, 228], [74, 190], [73, 204], [74, 165], [75, 216], [77, 220], [73, 208], [74, 210], [76, 215], [74, 195], [75, 200], [73, 215], [76, 229], [78, 240], [75, 207], [73, 205], [77, 208], [74, 185], [72, 190], [74, 170], [72, 208], [71, 225], [73, 190], [75, 225], [73, 185], [67, 180], [67, 165], [76, 240], [74, 220], [73, 212], [70, 163], [75, 215], [70, 175], [72, 205], [77, 210], [79, 205], [78, 208], [74, 215], [75, 180], [75, 200], [78, 230], [76, 211], [75, 230], [69, 190], [75, 220], [72, 180], [75, 205], [73, 190], [74, 180], [75, 205], [75, 190], [73, 195]]
np_baseball = np.array(baseball)
# For loop over np_height
for x in np_height:
print(f"{x} inches")
74 inches 74 inches 72 inches 72 inches 73 inches 69 inches 69 inches 71 inches 76 inches 71 inches 73 inches 73 inches 74 inches 74 inches 69 inches 70 inches 73 inches 75 inches 78 inches 79 inches 76 inches 74 inches 76 inches 72 inches 71 inches 75 inches 77 inches 74 inches 73 inches 74 inches 78 inches 73 inches 75 inches 73 inches 75 inches 75 inches 74 inches 69 inches 71 inches 74 inches 73 inches 73 inches 76 inches 74 inches 74 inches 70 inches 72 inches 77 inches 74 inches 70 inches 73 inches 75 inches 76 inches 76 inches 78 inches 74 inches 74 inches 76 inches 77 inches 81 inches 78 inches 75 inches 77 inches 75 inches 76 inches 74 inches 72 inches 72 inches 75 inches 73 inches 73 inches 73 inches 70 inches 70 inches 70 inches 76 inches 68 inches 71 inches 72 inches 75 inches 75 inches 75 inches 75 inches 68 inches 74 inches 78 inches 71 inches 73 inches 76 inches 74 inches 74 inches 79 inches 75 inches 73 inches 76 inches 74 inches 74 inches 73 inches 72 inches 74 inches 73 inches 74 inches 72 inches 73 inches 69 inches 72 inches 73 inches 75 inches 75 inches 73 inches 72 inches 72 inches 76 inches 74 inches 72 inches 77 inches 74 inches 77 inches 75 inches 76 inches 80 inches 74 inches 74 inches 75 inches 78 inches 73 inches 73 inches 74 inches 75 inches 76 inches 71 inches 73 inches 74 inches 76 inches 76 inches 74 inches 73 inches 74 inches 70 inches 72 inches 73 inches 73 inches 73 inches 73 inches 71 inches 74 inches 74 inches 72 inches 74 inches 71 inches 74 inches 73 inches 75 inches 75 inches 79 inches 73 inches 75 inches 76 inches 74 inches 76 inches 78 inches 74 inches 76 inches 72 inches 74 inches 76 inches 74 inches 75 inches 78 inches 75 inches 72 inches 74 inches 72 inches 74 inches 70 inches 71 inches 70 inches 75 inches 71 inches 71 inches 73 inches 72 inches 71 inches 73 inches 72 inches 75 inches 74 inches 74 inches 75 inches 73 inches 77 inches 73 inches 76 inches 75 inches 74 inches 76 inches 75 inches 73 inches 71 inches 76 inches 75 inches 72 inches 71 inches 77 inches 73 inches 74 inches 71 inches 72 inches 74 inches 75 inches 73 inches 72 inches 75 inches 75 inches 74 inches 72 inches 74 inches 71 inches 70 inches 74 inches 77 inches 77 inches 75 inches 75 inches 78 inches 75 inches 76 inches 73 inches 75 inches 75 inches 79 inches 77 inches 76 inches 71 inches 75 inches 74 inches 69 inches 71 inches 76 inches 72 inches 72 inches 70 inches 72 inches 73 inches 71 inches 72 inches 71 inches 73 inches 72 inches 73 inches 74 inches 74 inches 72 inches 75 inches 74 inches 74 inches 77 inches 75 inches 73 inches 72 inches 71 inches 74 inches 77 inches 75 inches 75 inches 75 inches 78 inches 78 inches 74 inches 76 inches 78 inches 76 inches 70 inches 72 inches 80 inches 74 inches 74 inches 71 inches 70 inches 72 inches 71 inches 74 inches 71 inches 72 inches 71 inches 74 inches 69 inches 76 inches 75 inches 75 inches 76 inches 73 inches 76 inches 73 inches 77 inches 73 inches 72 inches 72 inches 77 inches 77 inches 71 inches 74 inches 74 inches 73 inches 78 inches 75 inches 73 inches 70 inches 74 inches 72 inches 73 inches 73 inches 75 inches 75 inches 74 inches 76 inches 73 inches 74 inches 75 inches 75 inches 72 inches 73 inches 73 inches 72 inches 74 inches 78 inches 76 inches 73 inches 74 inches 75 inches 70 inches 75 inches 71 inches 72 inches 78 inches 75 inches 73 inches 73 inches 71 inches 75 inches 77 inches 72 inches 69 inches 73 inches 74 inches 72 inches 70 inches 75 inches 70 inches 72 inches 72 inches 74 inches 73 inches 74 inches 76 inches 75 inches 80 inches 72 inches 75 inches 73 inches 74 inches 74 inches 73 inches 75 inches 75 inches 71 inches 73 inches 75 inches 74 inches 74 inches 72 inches 74 inches 74 inches 74 inches 73 inches 76 inches 75 inches 72 inches 73 inches 73 inches 73 inches 72 inches 72 inches 72 inches 72 inches 71 inches 75 inches 75 inches 74 inches 73 inches 75 inches 79 inches 74 inches 76 inches 73 inches 74 inches 74 inches 72 inches 74 inches 74 inches 75 inches 78 inches 74 inches 74 inches 74 inches 77 inches 70 inches 73 inches 74 inches 73 inches 71 inches 75 inches 71 inches 72 inches 77 inches 74 inches 70 inches 77 inches 73 inches 72 inches 76 inches 71 inches 76 inches 78 inches 75 inches 73 inches 78 inches 74 inches 79 inches 75 inches 76 inches 72 inches 75 inches 75 inches 70 inches 72 inches 70 inches 74 inches 71 inches 76 inches 73 inches 76 inches 71 inches 69 inches 72 inches 72 inches 69 inches 73 inches 69 inches 73 inches 74 inches 74 inches 72 inches 71 inches 72 inches 72 inches 76 inches 76 inches 76 inches 74 inches 76 inches 75 inches 71 inches 72 inches 71 inches 73 inches 75 inches 76 inches 75 inches 71 inches 75 inches 74 inches 72 inches 73 inches 73 inches 73 inches 73 inches 76 inches 72 inches 76 inches 73 inches 73 inches 73 inches 75 inches 75 inches 77 inches 73 inches 72 inches 75 inches 70 inches 74 inches 72 inches 80 inches 71 inches 71 inches 74 inches 74 inches 73 inches 75 inches 76 inches 73 inches 77 inches 72 inches 73 inches 77 inches 76 inches 71 inches 75 inches 73 inches 74 inches 77 inches 71 inches 72 inches 73 inches 69 inches 73 inches 70 inches 74 inches 76 inches 73 inches 73 inches 75 inches 73 inches 79 inches 74 inches 73 inches 74 inches 77 inches 75 inches 74 inches 73 inches 77 inches 73 inches 77 inches 74 inches 74 inches 73 inches 77 inches 74 inches 77 inches 75 inches 77 inches 75 inches 71 inches 74 inches 70 inches 79 inches 72 inches 72 inches 70 inches 74 inches 74 inches 72 inches 73 inches 72 inches 74 inches 74 inches 76 inches 82 inches 74 inches 74 inches 70 inches 73 inches 73 inches 74 inches 77 inches 72 inches 76 inches 73 inches 73 inches 72 inches 74 inches 74 inches 71 inches 72 inches 75 inches 74 inches 74 inches 77 inches 70 inches 71 inches 73 inches 76 inches 71 inches 75 inches 74 inches 72 inches 76 inches 79 inches 76 inches 73 inches 76 inches 78 inches 75 inches 76 inches 72 inches 72 inches 73 inches 73 inches 75 inches 71 inches 76 inches 70 inches 75 inches 74 inches 75 inches 73 inches 71 inches 71 inches 72 inches 73 inches 73 inches 72 inches 69 inches 73 inches 78 inches 71 inches 73 inches 75 inches 76 inches 70 inches 74 inches 77 inches 75 inches 79 inches 72 inches 77 inches 73 inches 75 inches 75 inches 75 inches 73 inches 73 inches 76 inches 77 inches 75 inches 70 inches 71 inches 71 inches 75 inches 74 inches 69 inches 70 inches 75 inches 72 inches 75 inches 73 inches 72 inches 72 inches 72 inches 76 inches 75 inches 74 inches 69 inches 73 inches 72 inches 72 inches 75 inches 77 inches 76 inches 80 inches 77 inches 76 inches 79 inches 71 inches 75 inches 73 inches 76 inches 77 inches 73 inches 76 inches 70 inches 75 inches 73 inches 75 inches 70 inches 69 inches 71 inches 72 inches 72 inches 73 inches 70 inches 70 inches 73 inches 76 inches 75 inches 72 inches 73 inches 79 inches 71 inches 72 inches 74 inches 74 inches 74 inches 72 inches 76 inches 76 inches 72 inches 72 inches 71 inches 72 inches 72 inches 70 inches 77 inches 74 inches 72 inches 76 inches 71 inches 76 inches 71 inches 73 inches 70 inches 73 inches 73 inches 72 inches 71 inches 71 inches 71 inches 72 inches 72 inches 74 inches 74 inches 74 inches 71 inches 72 inches 75 inches 72 inches 71 inches 72 inches 72 inches 72 inches 72 inches 74 inches 74 inches 77 inches 75 inches 73 inches 75 inches 73 inches 76 inches 72 inches 77 inches 75 inches 72 inches 71 inches 71 inches 75 inches 72 inches 73 inches 73 inches 71 inches 70 inches 75 inches 71 inches 76 inches 73 inches 68 inches 71 inches 72 inches 74 inches 77 inches 72 inches 76 inches 78 inches 81 inches 72 inches 73 inches 76 inches 72 inches 72 inches 74 inches 76 inches 73 inches 76 inches 75 inches 70 inches 71 inches 74 inches 72 inches 73 inches 76 inches 76 inches 73 inches 71 inches 68 inches 71 inches 71 inches 74 inches 77 inches 69 inches 72 inches 76 inches 75 inches 76 inches 75 inches 76 inches 72 inches 74 inches 76 inches 74 inches 72 inches 75 inches 78 inches 77 inches 70 inches 72 inches 79 inches 74 inches 71 inches 68 inches 77 inches 75 inches 71 inches 72 inches 70 inches 72 inches 72 inches 73 inches 72 inches 74 inches 72 inches 72 inches 75 inches 72 inches 73 inches 74 inches 72 inches 78 inches 75 inches 72 inches 74 inches 75 inches 75 inches 76 inches 74 inches 74 inches 73 inches 74 inches 71 inches 74 inches 75 inches 76 inches 74 inches 76 inches 76 inches 73 inches 75 inches 75 inches 74 inches 68 inches 72 inches 75 inches 71 inches 70 inches 72 inches 73 inches 72 inches 75 inches 74 inches 70 inches 76 inches 71 inches 82 inches 72 inches 73 inches 74 inches 71 inches 75 inches 77 inches 72 inches 74 inches 72 inches 73 inches 78 inches 77 inches 73 inches 73 inches 73 inches 73 inches 73 inches 76 inches 75 inches 70 inches 73 inches 72 inches 73 inches 75 inches 74 inches 73 inches 73 inches 76 inches 73 inches 75 inches 70 inches 77 inches 72 inches 77 inches 74 inches 75 inches 75 inches 75 inches 75 inches 72 inches 74 inches 71 inches 76 inches 71 inches 75 inches 76 inches 83 inches 75 inches 74 inches 76 inches 72 inches 72 inches 75 inches 75 inches 72 inches 77 inches 73 inches 72 inches 70 inches 74 inches 72 inches 74 inches 72 inches 71 inches 70 inches 71 inches 76 inches 74 inches 76 inches 74 inches 74 inches 74 inches 75 inches 75 inches 71 inches 71 inches 74 inches 77 inches 71 inches 74 inches 75 inches 77 inches 76 inches 74 inches 76 inches 72 inches 71 inches 72 inches 75 inches 73 inches 68 inches 72 inches 69 inches 73 inches 73 inches 75 inches 70 inches 70 inches 74 inches 75 inches 74 inches 74 inches 73 inches 74 inches 75 inches 77 inches 73 inches 74 inches 76 inches 74 inches 75 inches 73 inches 76 inches 78 inches 75 inches 73 inches 77 inches 74 inches 72 inches 74 inches 72 inches 71 inches 73 inches 75 inches 73 inches 67 inches 67 inches 76 inches 74 inches 73 inches 70 inches 75 inches 70 inches 72 inches 77 inches 79 inches 78 inches 74 inches 75 inches 75 inches 78 inches 76 inches 75 inches 69 inches 75 inches 72 inches 75 inches 73 inches 74 inches 75 inches 75 inches 73 inches
# For loop over np_baseball
for x in np.nditer(np_baseball):
print(x)
74 180 74 215 72 210 72 210 73 188 69 176 69 209 71 200 76 231 71 180 73 188 73 180 74 185 74 160 69 180 70 185 73 189 75 185 78 219 79 230 76 205 74 230 76 195 72 180 71 192 75 225 77 203 74 195 73 182 74 188 78 200 73 180 75 200 73 200 75 245 75 240 74 215 69 185 71 175 74 199 73 200 73 215 76 200 74 205 74 206 70 186 72 188 77 220 74 210 70 195 73 200 75 200 76 212 76 224 78 210 74 205 74 220 76 195 77 200 81 260 78 228 75 270 77 200 75 210 76 190 74 220 72 180 72 205 75 210 73 220 73 211 73 200 70 180 70 190 70 170 76 230 68 155 71 185 72 185 75 200 75 225 75 225 75 220 68 160 74 205 78 235 71 250 73 210 76 190 74 160 74 200 79 205 75 222 73 195 76 205 74 220 74 220 73 170 72 185 74 195 73 220 74 230 72 180 73 220 69 180 72 180 73 170 75 210 75 215 73 200 72 213 72 180 76 192 74 235 72 185 77 235 74 210 77 222 75 210 76 230 80 220 74 180 74 190 75 200 78 210 73 194 73 180 74 190 75 240 76 200 71 198 73 200 74 195 76 210 76 220 74 190 73 210 74 225 70 180 72 185 73 170 73 185 73 185 73 180 71 178 74 175 74 200 72 204 74 211 71 190 74 210 73 190 75 190 75 185 79 290 73 175 75 185 76 200 74 220 76 170 78 220 74 190 76 220 72 205 74 200 76 250 74 225 75 215 78 210 75 215 72 195 74 200 72 194 74 220 70 180 71 180 70 170 75 195 71 180 71 170 73 206 72 205 71 200 73 225 72 201 75 225 74 233 74 180 75 225 73 180 77 220 73 180 76 237 75 215 74 190 76 235 75 190 73 180 71 165 76 195 75 200 72 190 71 190 77 185 73 185 74 205 71 190 72 205 74 206 75 220 73 208 72 170 75 195 75 210 74 190 72 211 74 230 71 170 70 185 74 185 77 241 77 225 75 210 75 175 78 230 75 200 76 215 73 198 75 226 75 278 79 215 77 230 76 240 71 184 75 219 74 170 69 218 71 190 76 225 72 220 72 176 70 190 72 197 73 204 71 167 72 180 71 195 73 220 72 215 73 185 74 190 74 205 72 205 75 200 74 210 74 215 77 200 75 205 73 211 72 190 71 208 74 200 77 210 75 232 75 230 75 210 78 220 78 210 74 202 76 212 78 225 76 170 70 190 72 200 80 237 74 220 74 170 71 193 70 190 72 150 71 220 74 200 71 190 72 185 71 185 74 200 69 172 76 220 75 225 75 190 76 195 73 219 76 190 73 197 77 200 73 195 72 210 72 177 77 220 77 235 71 180 74 195 74 195 73 190 78 230 75 190 73 200 70 190 74 190 72 200 73 200 73 184 75 200 75 180 74 219 76 187 73 200 74 220 75 205 75 190 72 170 73 160 73 215 72 175 74 205 78 200 76 214 73 200 74 190 75 180 70 205 75 220 71 190 72 215 78 235 75 191 73 200 73 181 71 200 75 210 77 240 72 185 69 165 73 190 74 185 72 175 70 155 75 210 70 170 72 175 72 220 74 210 73 205 74 200 76 205 75 195 80 240 72 150 75 200 73 215 74 202 74 200 73 190 75 205 75 190 71 160 73 215 75 185 74 200 74 190 72 210 74 185 74 220 74 190 73 202 76 205 75 220 72 175 73 160 73 190 73 200 72 229 72 206 72 220 72 180 71 195 75 175 75 188 74 230 73 190 75 200 79 190 74 219 76 235 73 180 74 180 74 180 72 200 74 234 74 185 75 220 78 223 74 200 74 210 74 200 77 210 70 190 73 177 74 227 73 180 71 195 75 199 71 175 72 185 77 240 74 210 70 180 77 194 73 225 72 180 76 205 71 193 76 230 78 230 75 220 73 200 78 249 74 190 79 208 75 245 76 250 72 160 75 192 75 220 70 170 72 197 70 155 74 190 71 200 76 220 73 210 76 228 71 190 69 160 72 184 72 180 69 180 73 200 69 176 73 160 74 222 74 211 72 195 71 200 72 175 72 206 76 240 76 185 76 260 74 185 76 221 75 205 71 200 72 170 71 201 73 205 75 185 76 205 75 245 71 220 75 210 74 220 72 185 73 175 73 170 73 180 73 200 76 210 72 175 76 220 73 206 73 180 73 210 75 195 75 200 77 200 73 164 72 180 75 220 70 195 74 205 72 170 80 240 71 210 71 195 74 200 74 205 73 192 75 190 76 170 73 240 77 200 72 205 73 175 77 250 76 220 71 224 75 210 73 195 74 180 77 245 71 175 72 180 73 215 69 175 73 180 70 195 74 230 76 230 73 205 73 215 75 195 73 180 79 205 74 180 73 190 74 180 77 190 75 190 74 220 73 210 77 255 73 190 77 230 74 200 74 205 73 210 77 225 74 215 77 220 75 205 77 200 75 220 71 197 74 225 70 187 79 245 72 185 72 185 70 175 74 200 74 180 72 188 73 225 72 200 74 210 74 245 76 213 82 231 74 165 74 228 70 210 73 250 73 191 74 190 77 200 72 215 76 254 73 232 73 180 72 215 74 220 74 180 71 200 72 170 75 195 74 210 74 200 77 220 70 165 71 180 73 200 76 200 71 170 75 224 74 220 72 180 76 198 79 240 76 239 73 185 76 210 78 220 75 200 76 195 72 220 72 230 73 170 73 220 75 230 71 165 76 205 70 192 75 210 74 205 75 200 73 210 71 185 71 195 72 202 73 205 73 195 72 180 69 200 73 185 78 240 71 185 73 220 75 205 76 205 70 180 74 201 77 190 75 208 79 240 72 180 77 230 73 195 75 215 75 190 75 195 73 215 73 215 76 220 77 220 75 230 70 195 71 190 71 195 75 209 74 204 69 170 70 185 75 205 72 175 75 210 73 190 72 180 72 180 72 160 76 235 75 200 74 210 69 180 73 190 72 197 72 203 75 205 77 170 76 200 80 250 77 200 76 220 79 200 71 190 75 170 73 190 76 220 77 215 73 206 76 215 70 185 75 235 73 188 75 230 70 195 69 168 71 190 72 160 72 200 73 200 70 189 70 180 73 190 76 200 75 220 72 187 73 240 79 190 71 180 72 185 74 210 74 220 74 219 72 190 76 193 76 175 72 180 72 215 71 210 72 200 72 190 70 185 77 220 74 170 72 195 76 205 71 195 76 210 71 190 73 190 70 180 73 220 73 190 72 186 71 185 71 190 71 180 72 190 72 170 74 210 74 240 74 220 71 180 72 210 75 210 72 195 71 160 72 180 72 205 72 200 72 185 74 245 74 190 77 210 75 200 73 200 75 222 73 215 76 240 72 170 77 220 75 156 72 190 71 202 71 221 75 200 72 190 73 210 73 190 71 200 70 165 75 190 71 185 76 230 73 208 68 209 71 175 72 180 74 200 77 205 72 200 76 250 78 210 81 230 72 244 73 202 76 240 72 200 72 215 74 177 76 210 73 170 76 215 75 217 70 198 71 200 74 220 72 170 73 200 76 230 76 231 73 183 71 192 68 167 71 190 71 180 74 180 77 215 69 160 72 205 76 223 75 175 76 170 75 190 76 240 72 175 74 230 76 223 74 196 72 167 75 195 78 190 77 250 70 190 72 190 79 190 74 170 71 160 68 150 77 225 75 220 71 209 72 210 70 176 72 260 72 195 73 190 72 184 74 180 72 195 72 195 75 219 72 225 73 212 74 202 72 185 78 200 75 209 72 200 74 195 75 228 75 210 76 190 74 212 74 190 73 218 74 220 71 190 74 235 75 210 76 200 74 188 76 210 76 235 73 188 75 215 75 216 74 220 68 180 72 185 75 200 71 210 70 220 72 185 73 231 72 210 75 195 74 200 70 205 76 200 71 190 82 250 72 185 73 180 74 170 71 180 75 208 77 235 72 215 74 244 72 220 73 185 78 230 77 190 73 200 73 180 73 190 73 196 73 180 76 230 75 224 70 160 73 178 72 205 73 185 75 210 74 180 73 190 73 200 76 257 73 190 75 220 70 165 77 205 72 200 77 208 74 185 75 215 75 170 75 235 75 210 72 170 74 180 71 170 76 190 71 150 75 230 76 203 83 260 75 246 74 186 76 210 72 198 72 210 75 215 75 180 72 200 77 245 73 200 72 192 70 192 74 200 72 192 74 205 72 190 71 186 70 170 71 197 76 219 74 200 76 220 74 207 74 225 74 207 75 212 75 225 71 170 71 190 74 210 77 230 71 210 74 200 75 238 77 234 76 222 74 200 76 190 72 170 71 220 72 223 75 210 73 215 68 196 72 175 69 175 73 189 73 205 75 210 70 180 70 180 74 197 75 220 74 228 74 190 73 204 74 165 75 216 77 220 73 208 74 210 76 215 74 195 75 200 73 215 76 229 78 240 75 207 73 205 77 208 74 185 72 190 74 170 72 208 71 225 73 190 75 225 73 185 67 180 67 165 76 240 74 220 73 212 70 163 75 215 70 175 72 205 77 210 79 205 78 208 74 215 75 180 75 200 78 230 76 211 75 230 69 190 75 220 72 180 75 205 73 190 74 180 75 205 75 190 73 195
"""
EXERCICE 1
---
- Write a for loop that iterates over the rows of cars and on each iteration perform two print() calls: one to print out the row label and one to print out all of the rows contents.
"""
# Import cars data
import pandas as pd
cars = pd.read_csv('/Users/samiadrappeau/Documents/TBS/PYTHON-101/code/02-intermediate/cars.csv', index_col = 0) # ADAPT THE PATH TO YOURS
# Iterate over rows of cars
for label, row in cars.iterrows():
print(label)
print(row)
US cars_per_cap 809 country United States drives_right True Name: US, dtype: object AUS cars_per_cap 731 country Australia drives_right False Name: AUS, dtype: object JPN cars_per_cap 588 country Japan drives_right False Name: JPN, dtype: object IN cars_per_cap 18 country India drives_right False Name: IN, dtype: object RU cars_per_cap 200 country Russia drives_right True Name: RU, dtype: object MOR cars_per_cap 70 country Morocco drives_right True Name: MOR, dtype: object EG cars_per_cap 45 country Egypt drives_right True Name: EG, dtype: object
"""
EXERCICE 2
---
- Using the iterators lab and row, adapt the code in the for loop such that the first iteration prints out "US: 809", the second iteration "AUS: 731", and so on.
- The output should be in the form "country: cars_per_cap". Make sure to print out this exact string (with the correct spacing).
- You can use str() to convert your integer data to a string so that you can print it in conjunction with the country label.
"""
# Adapt for loop
for lab, row in cars.iterrows() :
print(lab)
print(row)
print(f"{lab}: {row['cars_per_cap']}")
print("-"*15)
US cars_per_cap 809 country United States drives_right True Name: US, dtype: object US: 809 --------------- AUS cars_per_cap 731 country Australia drives_right False Name: AUS, dtype: object AUS: 731 --------------- JPN cars_per_cap 588 country Japan drives_right False Name: JPN, dtype: object JPN: 588 --------------- IN cars_per_cap 18 country India drives_right False Name: IN, dtype: object IN: 18 --------------- RU cars_per_cap 200 country Russia drives_right True Name: RU, dtype: object RU: 200 --------------- MOR cars_per_cap 70 country Morocco drives_right True Name: MOR, dtype: object MOR: 70 --------------- EG cars_per_cap 45 country Egypt drives_right True Name: EG, dtype: object EG: 45 ---------------
"""
EXERCICE 3
---
- Use a for loop to add a new column, named COUNTRY, that contains a uppercase version of the country names in the "country" column. You can use the string method upper() for this.
- To see if your code worked, print out cars. Don't indent this code, so that it's not part of the for loop.
"""
# Code for loop that adds COUNTRY column
for x, row in cars.iterrows():
cars.loc[x, "COUNTRY"] = row['country'].upper()
# Print cars
print(cars)
cars_per_cap country drives_right COUNTRY US 809 United States True UNITED STATES AUS 731 Australia False AUSTRALIA JPN 588 Japan False JAPAN IN 18 India False INDIA RU 200 Russia True RUSSIA MOR 70 Morocco True MOROCCO EG 45 Egypt True EGYPT
"""
EXERCICE 4
---
- Replace the for loop with a one-liner that uses .apply(str.upper). The call should give the same result: a column COUNTRY should be added to cars, containing an uppercase version of the country names.
- As usual, print out cars to see the fruits of your hard labor
"""
# Use .apply(str.upper)
cars["COUNTRY_2"] = cars["country"].apply(str.upper)
# Print cars
cars
| cars_per_cap | country | drives_right | COUNTRY | COUNTRY_2 | |
|---|---|---|---|---|---|
| US | 809 | United States | True | UNITED STATES | UNITED STATES |
| AUS | 731 | Australia | False | AUSTRALIA | AUSTRALIA |
| JPN | 588 | Japan | False | JAPAN | JAPAN |
| IN | 18 | India | False | INDIA | INDIA |
| RU | 200 | Russia | True | RUSSIA | RUSSIA |
| MOR | 70 | Morocco | True | MOROCCO | MOROCCO |
| EG | 45 | Egypt | True | EGYPT | EGYPT |