December 2018
Beginner to intermediate
682 pages
18h 1m
English
Another way to complete this recipe, beginning after step 2, is by directly assigning new columns from the sex_age column without using the split method. The assign method may be used to add these new columns dynamically:
>>> age_group = wl_melt.sex_age.str.extract('(\d{2}[-+](?:\d{2})?)', expand=False)>>> sex = wl_melt.sex_age.str[0]>>> new_cols = {'Sex':sex, 'Age Group': age_group}>>> wl_tidy2 = wl_melt.assign(**new_cols) \ .drop('sex_age',axis='columns')>>> wl_tidy2.sort_index(axis=1).equals(wl_tidy.sort_index(axis=1))True
The Sex column is found in the exact same manner as done in step 5. Because we are not using split, the Age Group column must be extracted in a different manner. The extract method uses a complex regular ...