Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional statement / If statement with Dataframes

I am trying to assign a value to column 'Percentage' based on multiple columns 'Class' and 'Value'

Below is a link that has my dataframe: https://filebin.net/fo2wk7crmwf0fycc

This is the logic that I want to be applied:

If df['Class'] equals 2 or 3, and if df['Value'] is less than 0.5, set df['Percentage'] to 0
If df['Class'] equals 2 or 3, and if df['Value'] is > 0.5 and <= 0.7, set df['Percentage'] to 0.25
If df['Class'] equals 2 or 3, and if df['Value'] is > 0.7 and <= 0.9, set df['Percentage'] to 0.5
Else set df['Percentage'] to 1

Below is an example of the output I am looking for:

Class Value Percentage
2 0.01 0
2 0.6 0.25
3 0.9 0.5
3 3 1

Thank you

like image 831
user11427018 Avatar asked Apr 06 '26 21:04

user11427018


1 Answers

Numpy and searchsorted

When using searchsorted you don't need to include the boundaries like 0 and 1 in this case.

bins =  np.array([.5, .7, .9])
labels = np.array([0, .25, .5, 1])
cut = bins.searchsorted(df.Value)
results = labels[cut]

df.assign(Percentage=np.where(df['Class'].isin([2, 3]), results, 1))

       Class     Value  Percentage
0          2  0.000620         0.0
1          2  0.000620         0.0
2          3  0.001240         0.0
3          4  0.000620         1.0
4          5  0.000620         1.0
...      ...       ...         ...
14782      5  0.001178         1.0
14783      2  0.001116         0.0
14784      3  0.001178         0.0
14785      5  0.000310         1.0
14786      5  0.001116         1.0

[14787 rows x 3 columns]

Pandas cut

When using pd.cut you DO need the boundaries because Pandas will create intervals.

#                        / boundaries \
#                       ↓              ↓
cut = pd.cut(df.Value, [0, .5, .7, .9, 1], labels=[0, .25, .5, 1])

df.assign(Percentage=np.where(df['Class'].isin([2, 3]), cut, 1))

       Class     Value  Percentage
0          2  0.000620         0.0
1          2  0.000620         0.0
2          3  0.001240         0.0
3          4  0.000620         1.0
4          5  0.000620         1.0
...      ...       ...         ...
14782      5  0.001178         1.0
14783      2  0.001116         0.0
14784      3  0.001178         0.0
14785      5  0.000310         1.0
14786      5  0.001116         1.0

[14787 rows x 3 columns]
like image 76
piRSquared Avatar answered Apr 09 '26 14:04

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!