[英]Apply function on every row of a pandas dataframe
How can I apply the function point_rotation
function on Coordinates1
, Coordinates2
columns in the below dataframe: 如何在下面的数据
point_rotation
Coordinates1
, Coordinates2
列上应用point_rotation
函数:
def point_rotation(point):
"""
Changing axis
"""
xcor, ycor = (700, 0)
xcor1, ycor1 = (0, 0)
xpoint, ypoint = point
xnew = xpoint - xcor
ynew = ypoint - ycor
xnew = xcor1 + math.cos(math.radians(270)) * (xnew - xcor1) - math.sin(math.radians(90)) * (ynew - ycor1)
ynew = ycor1 + math.sin(math.radians(270)) * (xnew - xcor1) + math.cos(math.radians(90)) * (ynew - ycor1)
return round(xnew, 0), round(ynew, 0)
Here's my current dataframe: 这是我当前的数据框:
df = pd.DataFrame({'X_1': [-34.58, -15.78, -33.45, 4.60, 10.48],
'Y_1': [-58.66, -47.91, -70.66, -74.08, -66.86],
'X_2': [-3.58, -1.8, -3.5, 4.0, 1.48],
'Y_2': [-5.66, -4.1, -7.6, -7.8, -6.86]})
df['Coordinates1'] = list(zip(df.X_1, df.Y_1))
df['Coordinates2'] = list(zip(df.X_2, df.Y_2))
Output wanted: should have columns Coordinates3
and Coordinates4
which are basically derived from the point_rotation
function by passing columns Coordinates1
and Coordinates2
. 所需的输出:应该具有列
Coordinates3
和Coordinates4
,这些列基本上是通过传递column Coordinates1
和Coordinates2
从point_rotation
函数派生的。
I tried using the apply
function, but it throws me an error: too many values to unpack (expected 2)
. 我尝试使用
apply
函数,但它引发了一个错误: too many values to unpack (expected 2)
。
Thanks for the help! 谢谢您的帮助!
If you use list comprehension. 如果您使用列表理解。 Your new point will be a tuple (similar to how you have defined
Coordinates1
and Coordinates2
) 您的新点将是一个元组(类似于您定义
Coordinates1
和Coordinates2
)
df['Coordinates3'] = [point_rotation(point) for point in df['Coordinates1']]
apply
also seems to work :) apply
也似乎工作:)
df['Coordinates4'] = df['Coordinates2'].apply(point_rotation)
Just do 做就是了
df['Coordinates3'] = df['Coordinates1'].apply(point_rotation)
df['Coordinates4'] = df['Coordinates2'].apply(point_rotation)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.