You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+48-50
Original file line number
Diff line number
Diff line change
@@ -1,35 +1,33 @@
1
1
2
-
# Going further with dictionaries
2
+
# Going further with dictionaries
3
3
4
4
### Introduction
5
5
6
6
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.
7
7
8
8
### Objectives
9
9
10
-
You will be able to:
11
-
12
10
* Understand how the list data structure aligns with data in non-programming contexts
13
11
* Understand how the dictionary data structure aligns with data in non-programming contexts
14
12
* See some of the steps involved in getting data from a different format and into code
15
13
16
14
### From Google Sheet to Local File
17
15
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.
19
17
20
-

18
+
<imgsrc="./countries-cities.png"width="500">
21
19
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.
23
21
24
-

22
+
<imgsrc="./download-xls.png"width="600">
25
23
26
24
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.
27
25
28
-
### From Local File to Python
26
+
### From Local File to Python
29
27
30
28
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.
31
29
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.
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`.
52
53
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.
54
55
55
56
Here is the code again, with some comments, if you are interested.
56
57
57
58
58
59
```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
60
61
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
62
63
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
0 commit comments