pandas 如何读取列并将函数作为元组应用于每个单元格?

qyzbxkaa  于 2023-05-12  发布在  其他
关注(0)|答案(2)|浏览(136)

我试图分析一个坐标为(X,Y)的数据库。我需要读取该列中的每个数据,如果是“Y”,则将其分类为北或南,如果是“X”,则将其分类为东或西。所以基本上我想做的是读取该列中的每个数据,并根据坐标X和Y应用其中一个值。
我的df是这样的(它和xlsx文档,但我会尝试做一些类似的东西)

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.

我试着将一个函数应用于整个列,然后用上一个函数的结果向 Dataframe 添加一个新列,我可能有这个想法,但我不知道如何实现。

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)
oxf4rvwz

oxf4rvwz1#

您可以通过使用pandas DataFrame或Series对象的apply方法将函数应用于元组列中的每个单元格来完成您在代码中试图实现的操作。
为了帮助您实现将每个坐标分类为北、南、东或西的目标,以下是一些示例代码:

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)
htzpubme

htzpubme2#

您仍然可以使用apply调用和一个单独的函数,请参阅下面调整后的代码:

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)

结果:

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

同样,您需要调整函数以返回您希望的文本

相关问题