我需要计算二维坐标在希尔伯特曲线上的距离。我使用了hilbertcurve包来构建自己的“hilbert”函数。这些坐标存储在一个数据框中(col_1和col_2)。如您所见,当应用于两个值(test)时,我的函数是可行的。
但是当通过apply函数逐行应用时,它就无法正常工作!这是为什么?我做错了什么?我需要一个附加列“hilbert”,其中包含从列“col_1”和“col_2”给定的x-和y-坐标的hilbert距离。
import pandas as pd
from hilbertcurve.hilbertcurve import HilbertCurve
df = pd.DataFrame({'ID': ['1', '2', '3'],
'col_1': [0, 2, 3],
'col_2': [1, 4, 5]})
def hilbert(x, y):
n = 2
p = 7
hilcur = HilbertCurve(p, n)
dist = hilcur.distance_from_coordinates([x, y])
return dist
test = hilbert(df.col_1[2], df.col_2[2])
df["hilbert"] = df.apply(hilbert(df.col_1, df.col_2), axis=0)
上一个命令出现了错误:
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
感谢您的帮助!
axis=0
的地方应该是axis=1
- 再试一次。 - Randy