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

Convert pandas column values based on groupings of values

I have a pandas columns with values 1.0, 2.0, 3.0, 4.0, and 5.0 like below:

0       5.0
1       2.0
2       3.0
3       3.0
4       5.0
       ... 
1039    5.0
1040    1.0
1041    2.0
1042    4.0
1043    1.0

I want rows with values 1.0 or 2.0 to all have a value of 1.0, 3.0 and 4.0 to become 2.0, and 5.0 to become 3.0. How could I re-assign the values based on these groupings. I was thinking np.where() at first but now I’m not sure how to implement that with np.where() logic because that seems like it would be better suited for conversion to a binary variable. Maybe just masking with .loc()?

Thanks.

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

>Solution :

Given your pattern, use simple arithmetics: add 1, get the floor division by 2:

df['new'] = df['col'].add(1).floordiv(2)

Or use cut:

df['new'] = pd.cut(df['col'], [0, 2, 4, 6], labels=[1, 2, 3])

Note that cut will give you a categorical type.

Example:

      col  new
0     5.0  3.0
1     2.0  1.0
2     3.0  2.0
3     3.0  2.0
4     5.0  3.0
1039  5.0  3.0
1040  1.0  1.0
1041  2.0  1.0
1042  4.0  2.0
1043  1.0  1.0
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