Skip to content

Commit 4d67655

Browse files
authored
Merge pull request #551 from tobyhodges/episode4-solutions
relocate solutions for episode 04
2 parents 37a1e55 + 139f332 commit 4d67655

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
@@ -49,43 +49,6 @@ previous steps visible.
4949

5050
## 04-data-types-and-format
5151

52-
### Challenge - Changing Types
53-
54-
- Try converting the column `plot_id` to floats using `surveys_df.plot_id.astype("float")`.
55-
Then, try converting the contents of the `weight` column to an integer type.
56-
What error messages does Pandas give you? What do these errors mean?
57-
58-
Pandas cannot convert types from float to int if the column contains NaN values.
59-
60-
### Challenge - Counting
61-
62-
- Count the number of missing values per column. Hint: The method `.count()` gives you the number of
63-
non-NA observations per column. Try looking to the `.isnull()` method.
64-
65-
```python
66-
for c in surveys_df.columns:
67-
print(c, len(surveys_df[surveys_df[c].isnull()]))
68-
```
69-
70-
Or, since we've been using the `pd.isnull` function so far:
71-
72-
```python
73-
for c in surveys_df.columns:
74-
print(c, len(surveys_df[pd.isnull(surveys_df[c])]))
75-
```
76-
77-
```output
78-
record_id 0
79-
month 0
80-
day 0
81-
year 0
82-
plot_id 0
83-
species_id 763
84-
sex 2511
85-
hindfoot_length 4111
86-
weight 3266
87-
```
88-
8952
### Writing Out Data to CSV
9053

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

0 commit comments

Comments
 (0)