vendredi 28 février 2020

List with condition to pick values less than 12

I want to give a condition to a list ("listy") that I created, and append its value to a new list ("listy2"). I have 20 points of x and y. Here is how I create listy

import numpy as np
import pandas as pd
x_y_data = pd.read_csv("x_y_points400_labeled_20pnts_csv.csv")
x = x_y_data.loc[:,'x']
y = x_y_data.loc[:,'y']
length_x = len(x)
listy=[]
for i in range(length_x):
    DistX = (x[i]-x)
    DistY = (y[i]-y)
    DistXY= np.sqrt(DistX**2+DistY**2)
    listy.append(DistXY)

Works fine until here, but.. I then tried to use a following code with the condition to get the values in listy that are less than 12:

listy2=[]
for j in listy:
    if j<12:
        listy2.append=(j)

np.savetxt("20pntslisty.csv", listy, delimiter=",", fmt='%s')
np.savetxt("20pntslisty_lessthan12.csv", listy2, delimiter=",", fmt='%s')

I get the following error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Can you help me fix this ?

here are values of x,y

    x   y
783.75  678.25
799.875 677.75
816.75  676.625
775 673.125
791.75  673.375
806.875 672.5
784 668
800.25  668.125
814.75  665.375
775.25  663.5
792.375 662.875
804.875 661
784.75  657.375

and here are values of listy:

 0 16.13275008 33.03998525 10.14042036 9.368330961 23.82914445 10.25304833 19.35886425 33.56732973 17.02388029 17.62898891 27.27321259 20.89893837
16.13275008 0   16.91245843 25.30130925 9.228014413 8.75    18.63003288 9.632302425 19.3495801  28.4508897  16.65880023 17.48034611 25.37530788
33.03998525 16.91245843 0   41.89644973 25.21036493 10.7019274  33.8666964  18.56071119 11.42639488 43.52603388 27.98576647 19.62539809 37.34384153
10.14042036 25.30130925 41.89644973 0   16.75186557 31.88112686 10.35691194 25.74028943 40.49845676 9.628246206 20.17307921 32.24176251 18.52363355
9.368330961 9.228014413 25.21036493 16.75186557 0   15.15028878 9.431496435 9.990620601 24.35159132 19.22929081 10.51858474 18.03902021 17.4642492
23.82914445 8.75    10.7019274  31.88112686 15.15028878 0   23.31342156 7.939222254 10.61985169 32.88070293 17.40375319 11.67261753 26.80076958
10.25304833 18.63003288 33.8666964  10.35691194 9.431496435 23.31342156 0   16.25048076 30.8618393  9.839334327 9.818668443 22.01739369 10.6514377
19.35886425 9.632302425 18.56071119 25.74028943 9.990620601 7.939222254 16.25048076 0   14.75847214 25.42421336 9.464572098 8.494483504 18.86299287
33.56732973 19.3495801  11.42639488 40.49845676 24.35159132 10.61985169 30.8618393  14.75847214 0   39.54447654 22.51423161 10.80075229 31.04834939
17.02388029 28.4508897  43.52603388 9.628246206 19.22929081 32.88070293 9.839334327 25.42421336 39.54447654 0   17.13640131 29.7302981  11.30334574
17.62898891 16.65880023 27.98576647 20.17307921 10.51858474 17.40375319 9.818668443 9.464572098 22.51423161 17.13640131 0   12.63984276 9.401628848
27.27321259 17.48034611 19.62539809 32.24176251 18.03902021 11.67261753 22.01739369 8.494483504 10.80075229 29.7302981  12.63984276 0   20.44886916
20.89893837 25.37530788 37.34384153 18.52363355 17.4642492  26.80076958 10.6514377  18.86299287 31.04834939 11.30334574 9.401628848 20.44886916 0

Aucun commentaire:

Enregistrer un commentaire