mardi 10 novembre 2020

How to extract a string from tuple index based on if-else condition in pandas?

My dataframe df.shape (105,6)

import pandas as pd
import io
import ast

     """  Ccom  Wcom  Lcom  Dcom  Total    Winner
(A1, A2)  -1.0   1.0   1.0   0.0    1.0    (A1, A2) 
(A1, A3)  -1.0  -1.0   1.0   1.0    0.0    (A1, A3)
(A1, A4)   1.0  -1.0   1.0   1.0    2.0    (A1, A4)
(A1, A5)  -1.0  -1.0  -1.0   1.0   -2.0    (A1, A5)
(A1, A6)  -1.0  -1.0   0.0   1.0   -1.0    (A1, A6) """

I want the name of index (viz tuple) in Winner column of dataframe based on the condition

if(df.Total > 0) --> Winner left i.e Winner = A1
elif(df.Total < 0) --> Winner right one i.e Winner = A2
elif(df.Total == 0 ) --> Winner='Draw' And keep a count of draw in both A1 and A2

Output should be something like:

           Ccom  Wcom  Lcom  Dcom  Total    Winner
(A1, A2)  -1.0   1.0   1.0   0.0    1.0       A1 
(A1, A3)  -1.0  -1.0   1.0   1.0    0.0      Draw
(A1, A4)   1.0  -1.0   1.0   1.0    2.0       A1
(A1, A5)  -1.0  -1.0  -1.0   1.0   -2.0       A5
(A1, A6)  -1.0  -1.0   0.0   1.0   -1.0       A6

Aucun commentaire:

Enregistrer un commentaire