Lab4-1_BA

Berent Aldikacti

09/14/20

In [1]:
import pandas as pd
surveys_df = pd.read_csv("data/surveys.csv")
In [2]:
type(surveys_df)
Out[2]:
pandas.core.frame.DataFrame
In [3]:
surveys_df.dtypes
Out[3]:
record_id            int64
month                int64
day                  int64
year                 int64
plot_id              int64
species_id          object
sex                 object
hindfoot_length    float64
weight             float64
dtype: object

Challenge - Changing Types

In [4]:
surveys_df.plot_id.astype("float")
Out[4]:
0         2.0
1         3.0
2         2.0
3         7.0
4         3.0
         ... 
35544    15.0
35545    15.0
35546    10.0
35547     7.0
35548     5.0
Name: plot_id, Length: 35549, dtype: float64
In [5]:
surveys_df.weight.astype("int64")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b49776cb6b44> in <module>
----> 1 surveys_df.weight.astype("int64")

~/miniconda3/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors)
   5541         else:
   5542             # else, only a single dtype is given
-> 5543             new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,)
   5544             return self._constructor(new_data).__finalize__(self, method="astype")
   5545 

~/miniconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, copy, errors)
    593         self, dtype, copy: bool = False, errors: str = "raise"
    594     ) -> "BlockManager":
--> 595         return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
    596 
    597     def convert(

~/miniconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, align_keys, **kwargs)
    404                 applied = b.apply(f, **kwargs)
    405             else:
--> 406                 applied = getattr(b, f)(**kwargs)
    407             result_blocks = _extend_blocks(applied, result_blocks)
    408 

~/miniconda3/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors)
    592             vals1d = values.ravel()
    593             try:
--> 594                 values = astype_nansafe(vals1d, dtype, copy=True)
    595             except (ValueError, TypeError):
    596                 # e.g. astype_nansafe can fail on object-dtype of strings

~/miniconda3/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
    959 
    960         if not np.isfinite(arr).all():
--> 961             raise ValueError("Cannot convert non-finite values (NA or inf) to integer")
    962 
    963     elif is_object_dtype(arr):

ValueError: Cannot convert non-finite values (NA or inf) to integer

Challenge - Counting

In [37]:
surveys_df.isnull().sum()
Out[37]:
record_id             0
month                 0
day                   0
year                  0
plot_id               0
species_id          763
sex                2511
hindfoot_length    4111
weight             3266
dtype: int64