Aside from implicit conversions, explicit conversions are also important. It’s essential that the data is presented in a convenient and readable format.
One part of this process is naming columns. This is what the method set_axis() is for.
Categories in datasets can be found as names and can vary in length.
What are the implications of storing them like this?
To store information about categories in the best way possible, a separate dictionary file must be created. The dictionary file maps each category name to a number. This number will be used in place of the category name in the table.
There is often just one entry with a specific index value. It’s impossible to work with bits of data like this and reach statistical conclusions. This is why this data must be categorized — that is, combined into categories.
One way to categorize the data is to filter it by age group. For example: up to 18, 18–65, and over 65.
Classification rules like these can be conveniently represented in Python as functions that take parameters and return a category value.
The group function that we wrote and the apply() method can be used to return the group column based on a column with another index.
data['column_group'] = data['column'].apply(group)
When the value from a single column is insufficient for categorization, the function can be passed the whole row contents as a Series. A function that is given a whole row can also return a value from a specific column.
When processing rows instead of single values, the apply() method differs in two ways: