Using If/else In Pandas Series To Create New Series Based On Conditions
I have a pandas df. Say I have a column 'activity' which can be 'fun' or 'work' and I want to convert it to an integer. What I do is: df['activity_id'] = 1*(df['activity']=='fun')
Solution 1:
You can create a dictionary with id as the key and the string as the value and then use the map
series method to convert the integer to a string.
my_map = {1:'fun', 2:'work'}
df['activity']= df.activity_id.map(my_map)
Solution 2:
You could instead convert your activity
column to categorical dtype:
df['activity'] = pd.Categorical(df['activity'])
Then you would automatically have access to integer labels for the values via df['activity'].cat.codes
.
import pandas as pd
df = pd.DataFrame({'activity':['fun','work','fun']})
df['activity'] = pd.Categorical(df['activity'])
print(df['activity'].cat.codes)
0 0
1 1
2 0
dtype: int8
Meanwhile the string values can still be accessed as before while saving memory:
print(df)
still yields
activity
0fun1 work
2fun
Solution 3:
You could also use a dictionary and list comprehension to recalculate values for an entire column. This makes it easy to define the reverse mapping as well:
>>> import pandas as pd
>>> forward_map = {'fun': 1, 'work': 2}
>>> reverse_map = {v: k for k, v in forward_map.iteritems()}
>>> df = pd.DataFrame(
{'activity': ['work', 'work', 'fun', 'fun', 'work'],
'detail': ['reports', 'coding', 'hiking', 'games', 'secret games']})
>>> df
activity detail
0 work reports
1 work coding
2fun hiking3fun games4 work secret games
>>> df['activity'] = [forward_map[i] for i in df['activity']]
>>> df
activity detail
02 reports
12 coding
21 hiking
31 games
42 secret games
>>> df['activity'] = [reverse_map[i] for i in df['activity']]
>>> df
activity detail
0 work reports
1 work coding
2fun hiking3fun games4 work secret games
Post a Comment for "Using If/else In Pandas Series To Create New Series Based On Conditions"