# How to iterate and modify rows in a dataframe( convert numerical to categorical)

I have a pandas dataframe like this

0    15.55
1    15.55
2    15.55
3    15.55
4    20.84
Name: Y1, dtype: float64


I want to convert the values of Y1 to categorical (i.e) if its greater than 18.25, I want it 1 else 0

This is what i tried so far

for temp in TRAIN_ID1:
train_ID1.loc[(train_ID1['Y1'] > 18.250000), 'Y1'] = 1
train_ID1.loc[(train_ID1['Y1'] < 18.250000), 'Y1'] = 0


But im getting an error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-118-2cccb791d834> in <module>()
1 for temp in train_ID1:
----> 2     train_ID1.loc[(train_ID1['Y1'] > 18.250000), 'Y1'] = 1
3     train_ID1.loc[(train_ID1['Y1'] < 18.250000), 'Y1'] = 0

~\Anaconda3\envs\deeplearning\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
621         key = com._apply_if_callable(key, self)
622         try:
--> 623             result = self.index.get_value(self, key)
624
625             if not is_scalar(result):

~\Anaconda3\envs\deeplearning\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
2558         try:
2559             return self._engine.get_value(s, k,
-> 2560                                           tz=getattr(series.dtype, 'tz', None))
2561         except KeyError as e1:
2562             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

KeyError: 'Y1'

• df['Y1'] = np.where(df['Y1']>18.25,1,0) – Sunnysinh Solanki Feb 25 '18 at 7:41

As the error suggests, you don't have a column called Y1. Hence the error. Here is my suggestion to fix this. Assuming you input data looks like this -

15.55
15.55
15.55
15.55
20.84


Read it in pandas this way -

import pandas as pd



Provide a column name for this -

df.columns = ['Y1']


If you have more columns, just fill the df.columns list accordingly.

Finally, use the pandas best practices as per their latest documentation to assign a new column -

df = df.assign(Y2= (df['Y1'] > 18.250000).astype(int))


Output

print(df)

Y1  Y2
0  15.55   0
1  15.55   0
2  15.55   0
3  15.55   0
4  20.84   1


Note: Since I don't have full visibility on what you are working on, I have assumed what might be the problems you are facing. If this doesn't work let me know.

• Thank you, i forgot to use df.columns as i copied the df from another dataframe. like df1=df['Y1'] – Srihari Feb 26 '18 at 17:46