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
# What happens if you ask for a column that doesn't exist?
91
96
surveys_df['speciess']
92
-
```
97
+
~~~
98
+
{: .language-python}
93
99
94
100
Python tells us what type of error it is in the traceback, at the bottom it says `KeyError: 'speciess'` which means that `speciess` is not a column name (or Key in the related python data type dictionary).
95
101
@@ -102,10 +108,11 @@ indexing. This means that the first element in an object is located at position
102
108
0. This is different from other tools like R and Matlab that index elements
103
109
within objects starting at 1.
104
110
105
-
```python
111
+
~~~
106
112
# Create a list of numbers:
107
113
a = [1, 2, 3, 4, 5]
108
-
```
114
+
~~~
115
+
{: .language-python}
109
116
110
117

111
118

@@ -143,22 +150,24 @@ DataFrame. To slice out a set of rows, you use the following syntax:
143
150
output. The stop bound is one step BEYOND the row you want to select. So if you
144
151
want to select rows 0, 1and2 your code would look like this:
145
152
146
-
```python
153
+
~~~
147
154
# Select rows 0, 1, 2 (row 3 is not selected)
148
155
surveys_df[0:3]
149
-
```
156
+
~~~
157
+
{: .language-python}
150
158
151
159
The stop bound in Python is different from what you might be used to in
152
160
languages like Matlab and R.
153
161
154
-
```python
162
+
~~~
155
163
# Select the first 5 rows (rows 0, 1, 2, 3, 4)
156
164
surveys_df[:5]
157
165
158
166
# Select the last element in the list
159
167
# (the slice starts at the last element, and ends at the end of the list)
160
168
surveys_df[-1:]
161
-
```
169
+
~~~
170
+
{: .language-python}
162
171
163
172
We can also reassign values within subsets of our DataFrame.
164
173
@@ -169,13 +178,14 @@ copying objects and the concept of referencing objects in Python.
169
178
170
179
Let's start with an example:
171
180
172
-
```python
181
+
~~~
173
182
# Using the 'copy() method'
174
183
true_copy_surveys_df = surveys_df.copy()
175
184
176
185
# Using the '=' operator
177
186
ref_surveys_df = surveys_df
178
-
```
187
+
~~~
188
+
{: .language-python}
179
189
180
190
You might think that the code `ref_surveys_df = surveys_df` creates a fresh
181
191
distinct copy of the `surveys_df` DataFrame object. However, using the `=`
@@ -190,20 +200,22 @@ DataFrame.
190
200
Let's look at what happens when we reassign the values within a subset of the
191
201
DataFrame that references another DataFrame object:
192
202
193
-
```python
203
+
~~~
194
204
# Assign the value `0` to the first three rows of data in the DataFrame
195
205
ref_surveys_df[0:3] = 0
196
-
```
206
+
~~~
207
+
{: .language-python}
197
208
198
209
Let's try the following code:
199
210
200
-
```python
211
+
~~~
201
212
# ref_surveys_df was created using the '=' operator
202
213
ref_surveys_df.head()
203
214
204
215
# surveys_df is the original dataframe
205
216
surveys_df.head()
206
-
```
217
+
~~~
218
+
{: .language-python}
207
219
208
220
What is the difference between these two dataframes?
209
221
@@ -230,9 +242,10 @@ the other will see the same changes to the reference object.
230
242
Okay, that's enough of that. Let's create a brand new clean dataframe from
231
243
the original data CSVfile.
232
244
233
-
```python
245
+
~~~
234
246
surveys_df = pd.read_csv("data/surveys.csv")
235
-
```
247
+
~~~
248
+
{: .language-python}
236
249
237
250
## Slicing Subsets of Rows and Columns in Python
238
251
@@ -247,10 +260,11 @@ To select a subset of rows **and** columns from our DataFrame, we can use the
247
260
`iloc` method. For example, we can select month, day and year (columns 2, 3
248
261
and4if we start counting at 1), like this:
249
262
250
-
```python
263
+
~~~
251
264
# iloc[row slicing, column slicing]
252
265
surveys_df.iloc[0:3, 1:4]
253
-
```
266
+
~~~
267
+
{: .language-python}
254
268
255
269
which gives the **output**
256
270
@@ -267,7 +281,7 @@ ask for 0:3, you are actually telling Python to start at index 0 and select rows
267
281
268
282
Let's explore some other ways to index and select subsets of data:
269
283
270
-
```python
284
+
~~~
271
285
# Select all columns for rows of index values 0 and 10
0 commit comments