Skip to content

Commit 139f332

Browse files
committed
relocate solutions for episode 04
1 parent 6e4b731 commit 139f332

2 files changed

Lines changed: 59 additions & 37 deletions

File tree

episodes/04-data-types-and-format.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,37 @@ surveys_df['plot_id'].astype("float")
247247
Next try converting `weight` to an integer. What goes wrong here? What is pandas telling you?
248248
We will talk about some solutions to this later.
249249

250+
::::::::::::::::::::::: solution
251+
252+
```python
253+
surveys_df['plot_id'].astype("float")
254+
```
255+
256+
```output
257+
0 2.0
258+
1 3.0
259+
2 2.0
260+
3 7.0
261+
4 3.0
262+
...
263+
35544 15.0
264+
35545 15.0
265+
35546 10.0
266+
35547 7.0
267+
35548 5.0
268+
```
269+
270+
```python
271+
surveys_df['weight'].astype("int")
272+
```
273+
274+
```error
275+
pandas.errors.IntCastingNaNError: Cannot convert non-finite values (NA or inf) to integer
276+
```
277+
278+
Pandas cannot convert types from float to int if the column contains NaN values.
279+
280+
::::::::::::::::::::::::::::::::
250281

251282
::::::::::::::::::::::::::::::::::::::::::::::::::
252283

@@ -348,7 +379,35 @@ Count the number of missing values per column.
348379
The method `.count()` gives you the number of non-NaN observations per column.
349380
Try looking to the `.isna()` method.
350381

382+
::::::::::::::::::::::: solution
383+
384+
```python
385+
for c in surveys_df.columns:
386+
print(c, len(surveys_df[surveys_df[c].isna()]))
387+
```
388+
389+
Or, since we've been using the `pd.isnull` function so far:
390+
391+
```python
392+
for c in surveys_df.columns:
393+
print(c, len(surveys_df[pd.isnull(surveys_df[c])]))
394+
```
395+
396+
```output
397+
record_id 0
398+
month 0
399+
day 0
400+
year 0
401+
plot_id 0
402+
species_id 763
403+
sex 2511
404+
hindfoot_length 4111
405+
weight 3266
406+
```
407+
408+
Note that `isnull` and `isna` are equivalent: they behave identically.
351409

410+
::::::::::::::::::::::::::::::::
352411

353412
:::::::::::::::::::::::::
354413

instructors/instructor-notes.md

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -299,43 +299,6 @@ surveys_df[~surveys_df["sex"].isin(['M', 'F'])]
299299

300300
## 04-data-types-and-format
301301

302-
### Challenge - Changing Types
303-
304-
- Try converting the column `plot_id` to floats using `surveys_df.plot_id.astype("float")`.
305-
Then, try converting the contents of the `weight` column to an integer type.
306-
What error messages does Pandas give you? What do these errors mean?
307-
308-
Pandas cannot convert types from float to int if the column contains NaN values.
309-
310-
### Challenge - Counting
311-
312-
- Count the number of missing values per column. Hint: The method `.count()` gives you the number of
313-
non-NA observations per column. Try looking to the `.isnull()` method.
314-
315-
```python
316-
for c in surveys_df.columns:
317-
print(c, len(surveys_df[surveys_df[c].isnull()]))
318-
```
319-
320-
Or, since we've been using the `pd.isnull` function so far:
321-
322-
```python
323-
for c in surveys_df.columns:
324-
print(c, len(surveys_df[pd.isnull(surveys_df[c])]))
325-
```
326-
327-
```output
328-
record_id 0
329-
month 0
330-
day 0
331-
year 0
332-
plot_id 0
333-
species_id 763
334-
sex 2511
335-
hindfoot_length 4111
336-
weight 3266
337-
```
338-
339302
### Writing Out Data to CSV
340303

341304
If the students have trouble generating the output, or anything happens with that, the folder

0 commit comments

Comments
 (0)