dimanche 27 juin 2021

How to append a column value to a list based on another column's condition from a dataframe (Python)?

I'm performing a classification task so out of all my features, I'd like to plot only BB against DD (see snippet of image). However, if the 't' value = 0, I'd like the data points to be yellow and if the 't' value = 1, I'd like the data points to be blue.

enter image description here

To accomplish this, I tried the following:

BB_0 = []
BB_1 = []
DD_0 = []
DD_1 = []
for i in range (len(data)):  #'data' is my dataframe
    if (data['t']==0):
        BB_0.append(data[BB]) # I'd like to append the vlaue of BB to BB_0 when 't'=0
    else:
        BB_1.append(data[BB])

# Then, I'd plot BB_0 and DD_0 using one color and BB_1 and DD_1 using another color. 

However, when I run this, it gives me a syntax error at the if statement. Is there an easier way to accomplish this?

Aucun commentaire:

Enregistrer un commentaire