Using Panda For Comparing Column Values And Creating Column Based On The Values In Compared Columns?
I am very new to python and pandas. I have following example CSV as input. ID Name Activity 1 AB Play 2 AD Hurt 3 cd Polite 4 CA Play I want to transform this
Solution 1:
You can use factorize
:
In [6]:
df['NumActivity'] = pd.factorize(df['Activity'])[0] +1
df
Out[6]:
ID Name Activity NumActivity
0 1 AB Play 1
1 2 AD Hurt 2
2 3 cd Polite 3
3 4 CA Play 1
Here factorize
returns a tuple containing array pairs:
In [8]:
pd.factorize(df['Activity'])
Out[8]:
(array([0, 1, 2, 0]), Index(['Play', 'Hurt', 'Polite'], dtype='object'))
So we index the first element using [0]
and seeing as you want your index to start from 1
we +1
to the values.
Post a Comment for "Using Panda For Comparing Column Values And Creating Column Based On The Values In Compared Columns?"