0

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:

enter image description here

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:\Users\DSAij\Documents\Projekt.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

enter image description here

martineau
  • 119,623
  • 25
  • 170
  • 301
Robxaa798
  • 171
  • 6
  • Please create a method that does your math function, it'll simplify SO MUCH your code – azro Mar 01 '21 at 22:21
  • Yeah I know. I've thought about it, but first I want to deal with this error. – Robxaa798 Mar 01 '21 at 22:26
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Show where the intermediate results differ from what you expected. – Prune Mar 01 '21 at 22:48
  • Please [include a minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of your MRE. Don't expect us to enter test data, or to build a test file. As posted, your code fails due to a private data file. – Prune Mar 01 '21 at 22:49

1 Answers1

1

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

dimitris_ps
  • 5,849
  • 3
  • 29
  • 55