[英]Apply function row wise to pandas dataframe
我必須從二維坐標計算希爾伯特曲線上的距離。 使用 hilbertcurve-package 我構建了自己的“hilbert”-function,來做到這一點。 坐標存儲在 dataframe(col_1 和 col_2)中。 如您所見,我的 function 在應用於兩個值(測試)時有效。
但是,當通過應用功能逐行應用時,它只是不起作用嗎? 為什么是這樣? 我在這里做錯了什么。 我需要一個附加列“hilbert”,其中包含“col_1”和“col_2”列中給出的 x 和 y 坐標的希爾伯特距離。
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().
謝謝您的幫助!
由於您在應用中有hilbert(df.col_1, df.col_2)
,因此立即嘗試使用這兩列的完整pd.Series
es 調用您的 function ,從而觸發該錯誤。 你應該做的是:
df.apply(lambda x: hilbert(x['col_1'], x['col_2']), axis=1)
這樣給定的 lambda function 將應用於每一行。
您必須將軸定義為 1,因為您想將 function 應用於行,而不是列。
您可以定義 lambda function 以僅對兩行應用希爾伯特,如下所示:
df['hilbert'] = df.apply(lambda row: hilbert(row['col_1'], row['col_2']), axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.