ÎncepețiÎncepe gratuit

Changing the data type to category

Since the rating column only has a few possible values, you'll change its data type to category in order to store the data more efficiently. You'll also specify a logical order for the categories, which will be useful for future exercises.

Acest exercițiu face parte din cursul

Analyzing Police Activity with pandas

Vezi cursul

Instrucțiuni pentru exercițiu

  • Create a CategoricalDtype object called cats that lists the weather ratings in a logical order: 'good', 'bad', 'worse'. Make sure to specify that the categories should be treated as ordered.
  • Use the cats object to change the data type of the rating column from object to category.
  • Examine the head of the rating column to confirm that the categories are logically ordered.

Exercițiu interactiv practic

Încearcă acest exercițiu completând acest cod de exemplu.

# Specify the logical order of the weather ratings
cats = pd.CategoricalDtype([____], ordered=____)

# Change the data type of 'rating' to category
weather['rating'] = weather.rating.____(____)

# Examine the head of 'rating'
print(____)
Editează și rulează codul