Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Classify the rows in dataframe based on their maximum

I have a dataframe which is very big. I don’t want to iterate over each row. I want to classify them based on the maximum of each row. If the values is greater than 0.45 is in class 3, if<0.2 in class 1, if >0.45 in class 3. Here is a sample of the dataframe:

import pandas as pd
import numpy as np
from pandas.tseries.holiday import USFederalHolidayCalendar as calendar
df = pd.DataFrame()
df['c0'] = [ 0.4656,  0.1530,0.1854 ]
df['c1'] = [ 0.4452, 0.2064, 0.1416]
df['c2'] = [0.4224 ,  0.4224, 0.1800]
df['max'] = df.max(axis=1)
df

And the dataframe which I want is:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Could you please help me with that?

>Solution :

IIUC, you could use np.select to assign class values to rows:

df['class'] = np.select([df['max']>0.45, df['max']<0.2], [3, 1], 2)

Output:

       c0      c1      c2     max  class
0  0.4656  0.4452  0.4224  0.4656      3
1  0.1530  0.2064  0.4224  0.4224      2
2  0.1854  0.1416  0.1800  0.1854      1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading