jeudi 22 février 2018

Multiple ifs for creating a new pandas column in dataframe

I have the following df dataframe in Pandas:

index_1    index_2    index_3
85         91         104
73         25         112
48         97         15
22         85         101

I want to add a new column called SEGMENT to the previous dataframe, based on the values of the indexes, like this:

if ((df['index_1'] > 90) & (df['index_2'] > 90) & (df['index_3'] > 90)) 
then **SEGMENT** should be **All**

if ((df['index_1'] > 90) & (df['index_2'] > 90))
then **SEGMENT** should be **Medium**

if ((df['index_2'] > 90) & (df['index_3'] > 90))
then **SEGMENT** should be **Medium high**

if ((df['index_2'] > 90))
then **SEGMENT** should be **Medium low**

if ((df['index_3'] > 90))
then **SEGMENT** should be **High**

if none of the indexes are greater than 90, put "None"

Desired result is this:

index_1    index_2    index_3    Segment
85         91         104        Medium high
73         25         112        High
48         97         15         None
22         85         101        High

How can I achieve this in Python with Pandas?

I know it is easy to do by putting each condition as a separate column, but I need all this together in the same column.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire