Skip to content

Commit 3bf586a

Browse files
author
Matt Stetz
committed
change population units
1 parent 50bd8d6 commit 3bf586a

5 files changed

+49
-579
lines changed

README.md

+48-50
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11

2-
# Going further with dictionaries
2+
# Going further with dictionaries
33

44
### Introduction
55

66
Now that we know a little bit about lists and dictionaries, we can take data in a different digital format, and move it into code. In this lesson, we'll see that in just a few lines of code, we can use Python to work with data in other formats. Then we'll learn a couple of other methods for working with dictionaries: `keys()`, `values()`, and the `dict()` constructor.
77

88
### Objectives
99

10-
You will be able to:
11-
1210
* Understand how the list data structure aligns with data in non-programming contexts
1311
* Understand how the dictionary data structure aligns with data in non-programming contexts
1412
* See some of the steps involved in getting data from a different format and into code
1513

1614
### From Google Sheet to Local File
1715

18-
For example, here is [our list of travel cities and countries](https://docs.google.com/spreadsheets/d/1BTJMMFH9t4p5UmHj5kiC6PGfMN6yaaaZkocx0mDqTK0/edit?usp=drive_web&ouid=111878893823071965889) in the form of a google document. If you click on the link, you will see our spreadsheet.
16+
For example, here is [our list of travel cities and countries](https://docs.google.com/spreadsheets/d/1kv8z2lZ3NLWbJcdE6ysd40BZLresdl5W6mWrtIunMn4/edit?usp=sharing) in the form of a google sheet. If you click on the link, you will see our spreadsheet.
1917

20-
![](./countries-cities.png)
18+
<img src="./countries-cities.png" width="500">
2119

22-
Now if we download this spreadsheet in the form of an .xlsx file we can start to work with it.
20+
You'll notice two additional columns: Population and Area. The Population column contains the population of the city in units of 1000 people. The Area column contains the area of the city in units of $km^2$. Now if we download this spreadsheet in the form of an .xlsx file we can start to work with it.
2321

24-
![](./download-xls.png)
22+
<img src="./download-xls.png" width="600">
2523

2624
We've already placed that file into this lesson, and you can see it [here](https://github.com/learn-co-curriculum/excel-to-python), where the contents of this lesson are also located.
2725

28-
### From Local File to Python
26+
### From Local File to Python
2927

3028
Now that we have this file in the folder we are working with, we can get this data into Python code in a few lines.
3129

32-
> ** Deep breath, soft eyes**: In the gray box below are four lines of code. They go over some topics we did not cover yet. So don't worry, if you don't follow everything right now. By the end of this unit, you will understand all of the code. For right now, it's fine to just have a slight sense of what's going on.
30+
> **Deep breath, soft eyes**: In the gray box below are four lines of code. They go over some topics we did not cover yet. So don't worry, if you don't follow everything right now. By the end of this unit, you will understand all of the code. For right now, it's fine to just have a slight sense of what's going on.
3331
3432

3533
```python
@@ -42,26 +40,29 @@ cities[0]
4240

4341

4442

45-
{'City': 'Solta', 'Country': 'Croatia', 'Population': 1700, 'Area': 59}
43+
{'City': 'Buenos Aires',
44+
'Country': 'Argentina',
45+
'Population': 2891,
46+
'Area': 203}
4647

4748

4849

4950
> Press shift+enter to run the code.
5051
5152
The code above relies on using an outside library called `pandas`, as it's good at reading excel files. A library is just a set of reusable functions. The `pandas` library is available for free online. We tell our current Jupyter notebook that we are about to use it with the line `import pandas`.
5253

53-
And that gives us an object, like a dictionary, which has a method on it called `read_excel`. Similar to how we can call `{'foo': 'bar'}.keys()`. That's the benefit of a library, we can get methods that do not come out of the box with Python. So we use the `read_excel` data to read our excel file, by providing the name of the file, `cities.xlsx`, and the preceding `./` just indicates that the file is found in the current folder. Finally with the line `travel_df.to_dict('records')` we return a list of our dictionaries representing our data. You can see that when we access the first element of this list, it returns our first dictionary.
54+
And that gives us an object, like a dictionary, which has a method in it called `read_excel`. Similar to how we can call `{'foo': 'bar'}.keys()`. That's the benefit of a library, we can get methods that do not come out of the box with Python. So we use the `read_excel` data to read our excel file, by providing the name of the file, `cities.xlsx`, and the preceding `./` just indicates that the file is found in the current folder. Finally with the line `travel_df.to_dict('records')` we return a list of our dictionaries representing our data. You can see that when we access the first element of this list, it returns our first dictionary.
5455

5556
Here is the code again, with some comments, if you are interested.
5657

5758

5859
```python
59-
# Here we use a library, which is some code not part of standard Python, to make this process easier
60+
# Here we use a library, which is some code not part of standard Python, to make this process easier
6061
import pandas
61-
# If we use the `import pandas` we have access to the pandas library
62+
# If we use the `import pandas` we have access to the pandas library
6263
travel_df = pandas.read_excel('./cities.xlsx')
63-
# We call the pandas.read_excel method and pass through the string './cities.xlsx' as the file is called cities.xlsx. By saying './' we are saying
64-
# go to the current folder, lists-lab, and find the 'cities.xlsx' file there
64+
# We call the pandas.read_excel method and pass through the string './cities.xlsx' as the file is called cities.xlsx. By saying './' we are saying
65+
# go to the current folder, excel-to-python, and find the 'cities.xlsx' file there
6566
cities = travel_df.to_dict('records')
6667
```
6768

@@ -73,45 +74,39 @@ cities
7374

7475

7576

76-
[{'Area': 59, 'City': 'Solta', 'Country': 'Croatia', 'Population': 1700},
77-
{'Area': 68, 'City': 'Greenville', 'Country': 'USA', 'Population': 84554},
78-
{'Area': 4758,
79-
'City': 'Buenos Aires',
77+
[{'City': 'Buenos Aires',
8078
'Country': 'Argentina',
81-
'Population': 13591863},
82-
{'Area': 3750,
83-
'City': 'Los Cabos',
84-
'Country': 'Mexico',
85-
'Population': 287651},
86-
{'Area': 33,
87-
'City': 'Walla Walla Valley',
88-
'Country': 'USA',
89-
'Population': 32237},
90-
{'Area': 200, 'City': 'Marakesh', 'Country': 'Morocco', 'Population': 928850},
91-
{'Area': 491,
92-
'City': 'Albuquerque',
93-
'Country': 'New Mexico',
94-
'Population': 559277},
95-
{'Area': 8300,
96-
'City': 'Archipelago Sea',
79+
'Population': 2891,
80+
'Area': 203},
81+
{'City': 'Toronto', 'Country': 'Canada', 'Population': 2732, 'Area': 630},
82+
{'City': 'Marakesh', 'Country': 'Morocco', 'Population': 929, 'Area': 230},
83+
{'City': 'Albuquerque', 'Country': 'USA', 'Population': 559, 'Area': 491},
84+
{'City': 'Los Cabos', 'Country': 'Mexico', 'Population': 288, 'Area': 3751},
85+
{'City': 'Greenville', 'Country': 'USA', 'Population': 93, 'Area': 68},
86+
{'City': 'Archipelago Sea',
9787
'Country': 'Finland',
98-
'Population': 60000},
99-
{'Area': 672,
100-
'City': 'Iguazu Falls',
101-
'Country': 'Argentina',
102-
'Population': 0},
103-
{'Area': 27, 'City': 'Salina Island', 'Country': 'Italy', 'Population': 4000},
104-
{'Area': 2731571, 'City': 'Toronto', 'Country': 'Canada', 'Population': 630},
105-
{'Area': 3194,
106-
'City': 'Pyeongchang',
88+
'Population': 60,
89+
'Area': 2000},
90+
{'City': 'Pyeongchang',
10791
'Country': 'South Korea',
108-
'Population': 2581000}]
92+
'Population': 44,
93+
'Area': 1464},
94+
{'City': 'Walla Walla Valley',
95+
'Country': 'USA',
96+
'Population': 33,
97+
'Area': 35},
98+
{'City': 'Salina Island', 'Country': 'Italy', 'Population': 3, 'Area': 26},
99+
{'City': 'Solta', 'Country': 'Croatia', 'Population': 2, 'Area': 59},
100+
{'City': 'Iguazu Falls',
101+
'Country': 'Argentina',
102+
'Population': 0,
103+
'Area': 2396}]
109104

110105

111106

112107
Look at that. Our variable `cities` is full of cities from our spreadsheet.
113108

114-
![](./countries-cities.png)
109+
<img src="./countries-cities.png" width="500">
115110

116111
And we got there in four lines of code.
117112

@@ -137,7 +132,10 @@ cities[0]
137132

138133

139134

140-
{'City': 'Solta', 'Country': 'Croatia', 'Population': 1700, 'Area': 59}
135+
{'City': 'Buenos Aires',
136+
'Country': 'Argentina',
137+
'Population': 2891,
138+
'Area': 203}
141139

142140

143141

@@ -181,7 +179,7 @@ cities[0].values()
181179

182180

183181

184-
dict_values(['Solta', 'Croatia', 1700, 59])
182+
dict_values(['Buenos Aires', 'Argentina', 2891, 203])
185183

186184

187185

@@ -193,15 +191,15 @@ list(cities[0].values())
193191

194192

195193

196-
['Solta', 'Croatia', 1700, 59]
194+
['Buenos Aires', 'Argentina', 2891, 203]
197195

198196

199197

200198
Once again, we call the method, and then coerce it into a list, by using the `list` function.
201199

202200
### Creating Dictionaries
203201

204-
So far, we have seen one way of creating dictionaries:
202+
So far, we have seen one way of creating dictionaries:
205203

206204

207205
```python

cities.xlsx

916 Bytes
Binary file not shown.

countries-cities.png

85.3 KB
Loading

download-xls.png

142 KB
Loading

0 commit comments

Comments
 (0)