1

I'm trying to analyze a database with coordinates (X,Y). I need to read each data in that column and classify it as either North or South if it's "Y" or East or West if it's "X". So basically what I want to do is read each data in that column and apply one of those values depending on the coordinate X and Y.

my df is something like this (it's and xlsx doc but ill try to make something alike)

df = [(0,23),(1,22),(4,39),(3,15)] #so i want to read each coordinate and if X is in a range between 0-23 say its East and if Y is in a range between 28-40 say its North.

I've tried to apply a function to the entire column and later adding a new column to the dataframe with the result from the previous function, I may have the idea but I don't know how to make it.

listing = list(0,23)
def calle():
    if calle in listing:
        return Oeste      #This is the code I tried to make a function with just one of the values
                      #So basically if the the coordinate is in that range(0-23) I want it to be west 

df1["Comienza calle"] = df1["Comienza calle"].apply(calle)
print(df1) #This is how i tried to apply the previous function

#And my idea is to add a new column with the result from that function
df1.insert(2, "Ubicación comienzo", ["Noroeste","Noreste","Suroeste","Sureste"], True)
print(df1) 
mousetail
  • 7,009
  • 4
  • 25
  • 45
rosvend
  • 21
  • 2

2 Answers2

1

You may do what you were attempting to achieve in your code by using the apply method of a pandas DataFrame or Series object to apply a function to each cell in a column of tuples.

To assist you accomplish your aim of categorising each coordinate as North, South, East, or West, here is some sample code:

import pandas as pd

class_ranges = {'X': [(0, 11.5, 'West'), (11.5, 23, 'East')],
                'Y': [(0, 28, 'South'), (28, 40, 'North')]}

def classify_coordinate(coord, coord_type):
    for (lower, upper, direction) in class_ranges[coord_type]:
        if lower <= coord <= upper:
            return direction
    return None

df = pd.read_excel('coordinates.xlsx')

df['X Direction'] = df['X'].apply(lambda x: classify_coordinate(x, 'X'))
df['Y Direction'] = df['Y'].apply(lambda y: classify_coordinate(y, 'Y'))

print(df)

Zufar Sunagatov
  • 668
  • 1
  • 6
  • 16
  • 2
    hey man thanks for the advice, however i cant seem to be able to achieve it, i got a link for my notebook , if you're avalaible please check it out https://deepnote.com/workspace/lp-data-science-75754d76-d127-4c44-9a49-f7c7018ca349/project/12-de-Mayo-Examen-final-18cc57f5-4380-4d69-8521-41f205c861b2/notebook/Notebook%201-f5e1815116be4b70bc73c31cd5d5d8a6 – rosvend May 11 '23 at 03:42
1

You can still use the apply call and a separate function, see adjusted code below:

import pandas as pd

# Function that we are going to use in the apply()
def coord_to_text(coord):
    x = coord[0]
    y = coord[1]

    # Fix the code below to match your expectations
    if x == 0:
        return "East"
    else:
        return "North"
    

# Create the dataframe using your values, but in a column named "coords"
df = pd.DataFrame({"coords": [(0,23),(1,22),(4,39),(3,15)]})

# Apply the funcion to the coords column, store results in a new column named text
df["text"] = df["coords"].apply(coord_to_text)

Result:

    coords  text
0   (0, 23) East
1   (1, 22) North
2   (4, 39) North
3   (3, 15) North

Again, you need to adjust the function to return the text as you wish it to be

Pep_8_Guardiola
  • 5,002
  • 1
  • 24
  • 35