I want to calculate the distance between two points and label them. The problem is that the code doesn’t work on more than 1 line. When there is 1 row, the program shows me result which I want:
This is an error when there is more than 1 line : “cannot convert the series to <class ‘float’>”
This is my code:
data = pd.read_csv (r'C:UsersDSAijDocumentsProjekt.csv') data.head() choices_1 = ['short','medium','long'] if not ((data['x_start'] < data['x_end']) & (data['y_start'] < data['y_end'])).empty: conditions_1 = [ ((math.sqrt((((data['x_end']) - (data['x_start']))**2) + ((data['y_end'])-(data['y_start']))**2)) < 5), ((math.sqrt((((data['x_end']) - (data['x_start']))**2) + ((data['y_end'])-(data['y_start']))**2)) >= 5 and (math.sqrt((((data['x_end']) - (data['x_start']))**2) + ((data['y_end'])-(data['y_start']))**2)) < 10), ((math.sqrt((((data['x_end']) - (data['x_start']))**2) + ((data['y_end'])-(data['y_start']))**2)) > 10)] data['Pass'] = np.select(conditions_1, choices_1) # if not ((data['x_start'] < data['x_end']) & (data['y_start'] > data['y_end'])).empty: # conditions_2 = [ # ((math.sqrt((((data['x_end']) - (data['x_start']))**2) + ((data['y_start'])-(data['y_end']))**2)) < 5), # ((math.sqrt((((data['x_end']) - (data['x_start']))**2) + ((data['y_start'])-(data['y_end']))**2)) >= 5 and # (math.sqrt((((data['x_end']) - (data['x_start']))**2) + ((data['y_start'])-(data['y_end']))**2)) < 10), # ((math.sqrt((((data['x_end']) - (data['x_start']))**2) + ((data['y_start'])-(data['y_end']))**2)) > 10)] # data['Pass'] = np.select(conditions_2, choices_1) # if not ((data['x_start'] > data['x_end']) & (data['y_start'] < data['y_end'])).empty: # conditions_3 = [ # ((math.sqrt((((data['x_start']) - (data['x_end']))**2) + ((data['y_end'])-(data['y_start']))**2)) < 5), # ((math.sqrt((((data['x_start']) - (data['x_end']))**2) + ((data['y_end'])-(data['y_start']))**2)) >= 5 and # (math.sqrt((((data['x_start']) - (data['x_end']))**2) + ((data['y_end'])-(data['y_start']))**2)) < 10), # ((math.sqrt((((data['x_start']) - (data['x_end']))**2) + ((data['y_end'])-(data['y_start']))**2)) > 10)] # data['Pass'] = np.select(conditions_3, choices_1) # if not ((data['x_start'] > data['x_end']) & (data['y_start'] > data['y_end'])).empty: # conditions_4 = [ # ((math.sqrt((((data['x_start']) - (data['x_end']))**2) + ((data['y_start'])-(data['y_end']))**2)) < 5), # ((math.sqrt((((data['x_start']) - (data['x_end']))**2) + ((data['y_start'])-(data['y_end']))**2)) >= 5 and # (math.sqrt((((data['x_start']) - (data['x_end']))**2) + ((data['y_start'])-(data['y_end']))**2)) < 10), # ((math.sqrt((((data['x_start']) - (data['x_end']))**2) + ((data['y_start'])-(data['y_end']))**2)) > 10)] #data['Pass'] = np.select(conditions_4, choices_1)
The part that is commented out is when the x_end is greater than x_start etc.
This is my data frame
Advertisement
Answer
Try this
import pandas as pd import numpy as np #create a function that calculates what you want (i.e distance in this case) def dist(x0, x1, y0, y1): return ((x1 - x0)**2 + (y1 - y0)**2)**(1/2) # Your dataframe (please provide this yourself next time) df = pd.DataFrame({'x_start':[24, 24, 24, 5], 'x_end':[12, 36, 12, 12], 'y_start':[35, 35, 95, 87], 'y_end':[57, 57, 57, 57]}) #this calculates the distance df['Pass'] = df.apply(lambda x: dist(x['x_start'], x['x_end'], x['y_start'], x['y_end']), axis=1) #this will apply your conditions df['Pass'] = np.select( [df['Pass']<5, (df['Pass']<10) & (df['Pass']>=5), df['Pass']>=10], ['short','medium','long'], default=np.nan) df