1

I would like to replace certain value-thresholds in a df with another value. For example all values between 1 and <3.3 should be summarized as 1. After that all values between >=3.3 and <10 should be summarized as 2 and so on.

I tried it like this: tndf is my df and tnn the column

tndf.loc[(tndf.tnn < 1), 'tnn'] = 0
tndf.loc[((tndf.tnn >= 1) | (tndf.tnn < 3.3)), 'tnn'] = 1
tndf.loc[((tndf.tnn >=3.3) | (tndf.tnn < 10)), 'tnn'] = 2
tndf.loc[((tndf.tnn >=10) | (tndf.tnn < 20)), 'tnn'] = 3
tndf.loc[((tndf.tnn >=20) | (tndf.tnn < 33.3)), 'tnn'] = 4
tndf.loc[((tndf.tnn >=33.3) | (tndf.tnn < 50)), 'tnn'] = 5
tndf.loc[((tndf.tnn >=50) | (tndf.tnn < 100)), 'tnn'] = 6
tndf.loc[(tndf.tnn == 100), 'tnn'] = 7

But every value at the end will be summarized as a 6. I think that's why because of the second part of each condition. But I don't know how to tell the program to only look in a specific range (for example from >=3.3 and <10).

2
  • Does that answer your question stackoverflow.com/questions/68195381/…? Commented Jul 6, 2021 at 12:33
  • Ah, thank you. So, if I am seeing this correctly then I only have to change | to &. Commented Jul 6, 2021 at 12:41

2 Answers 2

1

i will use np.where() here is the documentation: np.where()

import numpy as np
tnddf0=np.where((tndf.tnn < 1),0,"tnn")
tnddf1=np.where(((tndf.tnn >= 1) & (tndf.tnn < 3.3)),1,"tnn")
#and so on.... 
Sign up to request clarification or add additional context in comments.

Comments

1

To form categories like these use pd.cut

pd.cut(df.tnn, [0, 1, 3.3, 10, 20, 33.3, 50, 100], right=False, labels=range(0, 7))

Sample output of pd.cut

         tnn cat
0  76.518227   6
1  44.808386   5
2  46.798994   5
3  70.798699   6
4  67.301112   6
5  13.701745   3
6  47.310570   5
7  74.048936   6
8  37.904632   5
9  38.617358   5

OR

Use np.select. It is meant exactly for your use-case.

conditions = [tndf.tnn < 1, (tndf.tnn >= 1) | (tndf.tnn < 3.3)]
values = [0, 1]
np.select(conditions, values, default="unknown")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.