Fixing a data type
We saw in the previous exercise that the is_arrested
column currently has the object
data type. In this exercise, we'll change the data type to bool
, which is the most suitable type for a column containing True
and False
values.
Fixing the data type will enable us to use mathematical operations on the is_arrested
column that would not be possible otherwise.
Diese Übung ist Teil des Kurses
Analyzing Police Activity with pandas
Anleitung zur Übung
- Examine the head of the
is_arrested
column to verify that it containsTrue
andFalse
values and to check the column's data type. - Use the
.astype()
method to convertis_arrested
to abool
column. - Check the new data type of
is_arrested
to confirm that it is now abool
column.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# Examine the head of the 'is_arrested' column
print(ri.is_arrested.____)
# Change the data type of 'is_arrested' to 'bool'
ri['is_arrested'] = ri.is_arrested.____
# Check the data type of 'is_arrested'
print(____)