@@ -247,6 +247,37 @@ surveys_df['plot_id'].astype("float")
247247Next try converting ` weight ` to an integer. What goes wrong here? What is pandas telling you?
248248We 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.
348379The method ` .count() ` gives you the number of non-NaN observations per column.
349380Try 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
0 commit comments