Robxaa798
Robxaa798

Reputation: 161

Calculate the distance two points - from csv file

I want to measure the distance of these two points. Is there an easier way to calculate the distance? I try by np.select. I know that I would have to do 3 more conditions . This is error which I have :

enter image description here

I don't know what's going on with that.

This is my code :

data = pd.read_csv (r'C:\Users\Mijdasckj\Desktop\Projekt.csv')



choices_1 = ['short','medium','long']


if (data['x_start'] < data['x_end']) & (data['y_start'] < data['y_end']):
    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 & 
        (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)

And this is my csv file:

enter image description here

EDIT :

I wrote these 3 more conditions

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)


data.head()

Now I have another bug :

enter image description here

Previously, normally without these 3 conditions, it counted. Maybe I get it wrong this 'if ... .empty ? I added a few more lines to meet these conditions.

Upvotes: 0

Views: 142

Answers (1)

alexzander
alexzander

Reputation: 1875

you cant use this & in python as && in C++ or C.

change this:

(data['x_start'] < data['x_end']) & (data['y_start'] < data['y_end'])

to this:

(data['x_start'] < data['x_end']) and (data['y_start'] < data['y_end'])

Upvotes: 1

Related Questions