英文:
How to read a column and apply a function to each cell as a tuple?
问题
df = [(0,23),(1,22),(4,39),(3,15)]
listing = list(range(0, 24))
def classify_coordinates(coord):
if coord[0] in listing:
return "East"
elif coord[1] in range(28, 41):
return "North"
df_result = [(classify_coordinates(coord), classify_coordinates(coord)) for coord in df]
df1 = pd.DataFrame(df_result, columns=["Comienza calle", "Ubicación comienzo"])
print(df1)
英文:
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)
答案1
得分: 1
你可以通过使用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)
希望这对你有所帮助。
英文:
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)
答案2
得分: 1
import pandas as pd
# 在 apply() 中仍然可以使用 apply 调用和一个单独的函数,参见下面的调整后的代码:
# 我们将在 apply() 中使用的函数
def coord_to_text(coord):
x = coord[0]
y = coord[1]
# 修改下面的代码以符合您的期望
if x == 0:
return "东方"
else:
return "北方"
# 使用您的值创建数据框,但在名为 "coords" 的列中
df = pd.DataFrame({"coords": [(0,23),(1,22),(4,39),(3,15)]})
# 将函数应用于 coords 列,并将结果存储在名为 text 的新列中
df["text"] = df["coords"].apply(coord_to_text)
结果:
coords text
0 (0, 23) 东方
1 (1, 22) 北方
2 (4, 39) 北方
3 (3, 15) 北方
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论