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

Can someone please help me on how to do it

This is what i tried so far

for temp in TRAIN_ID1:
    train_ID1.loc[(train_ID1['Y1'] &gt; 18.250000), 'Y1'] = 1
    train_ID1.loc[(train_ID1['Y1'] &lt; 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)
&lt;ipython-input-118-2cccb791d834&gt; in &lt;module&gt;()
      1 for temp in train_ID1:
----&gt; 2     train_ID1.loc[(train_ID1['Y1'] &gt; 18.250000), 'Y1'] = 1
      3     train_ID1.loc[(train_ID1['Y1'] &lt; 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:
--&gt; 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,
-&gt; 2560                                           tz=getattr(series.dtype, 'tz', None))
   2561         except KeyError as e1:
   2562             if len(self) &gt; 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'
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

df = pd.read_csv('path/to/file.csv', header=None)


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'] &gt; 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.