Reputation: 259
I have this dataframe where the type
columns shows the type of a stuff
id type total
1 shoes 2
1 sandal 1
1 vest 2
1 tshirt 2
1 345 3
1 345 2
based on the type
column, I want to categorize the type and create a new column called category
.
The rules are as follow:
- shoes and sandal as shoes
- vest, we keep it as vest
- tshirt, we also keep it as tshirt
- other than that, I want to keep it as other
so the desired result will be as follow:
id type total category
1 shoes 2 shoes
1 sandal 1 shoes
1 vest 2 vest
1 tshirt 2 tshirt
1 345 3 other
1 345 2 other
how can I make it with python?
thanks in advance
Upvotes: 1
Views: 774
Reputation: 71560
Try using map
on a dictionary with fillna
:
>>> df['category'] = df['type'].map({'sandal': 'shoes', 'shoes': 'shoes', 'vest': 'vest', 'tshirt': 'tshirt'}).fillna('other')
>>> df
id type total category
0 1 shoes 2 shoes
1 1 sandal 1 shoes
2 1 vest 2 vest
3 1 tshirt 2 tshirt
4 1 345 3 other
5 1 345 2 other
>>>
As mentioned in the documentation of both pages (links shown above), map
maps and replaces the values in the Series
that are the keys of the dictionary to the values of the dictionary. Whereas if there are values in there series that aren't mapped in the dictionary, it would give NaN
s, so you could just do fillna
to replace the NaN
s with others
.
Upvotes: 6