向量化pandas中的函数

向量化pandas中的函数,第1张

向量化pandas中的函数

为了矢量化此代码,您将需要对完整的数据框进行 *** 作,而不要对单个经纬度进行 *** 作。我对此做了尝试。我需要结果df和一个新函数h2,

import numpy as npdef h2(df, p):    inrad = df.applymap(radians)    dlon = inrad.longitude-inrad.longitude[p]    dlat = inrad.latitude-inrad.latitude[p]    lat1 = pd.Series(index = df.index, data = [df.latitude[p] for i in range(len(df.index))])    a = np.sin(dlat/2)*np.sin(dlat/2) + np.cos(df.latitude) * np.cos(lat1) * np.sin(dlon/2)**2    c = 2 * 1/np.sin(np.sqrt(a))    km = 6367 * c    return kmdf = df.set_index('Provider ID')df = df.astype(float)df2 = pd.Dataframe(index = df.index, columns = df.index)for c in df2.columns:    df2[c] = h2(df, c)print (df2)

这应该产生(我不确定我是否有正确答案……我的目标是对代码进行矢量化处理)

Provider ID         10001         10005         10006         10007  Provider ID    10001      inf  5.021936e+05  5.270062e+05  1.649088e+06   10005        5.021936e+05inf  9.294868e+05  4.985233e+05   10006        5.270062e+05  9.294868e+05inf  4.548412e+05   10007        1.649088e+06  4.985233e+05  4.548412e+05inf   10008        1.460299e+06  5.777248e+05  5.246954e+05  3.638231e+06   10011        6.723581e+05  2.004199e+06  1.027439e+06  6.394402e+05   10012        4.559090e+05  3.265536e+06  7.573411e+05  4.694125e+05   10016        7.680036e+05  1.429573e+06  9.105474e+05  7.517467e+05   10018        7.096548e+05  1.733554e+06  1.020976e+06  6.701920e+05   10019        5.436342e+05  9.278739e+05  2.891822e+07  4.638858e+05Provider ID         10008         10011         10012         10016  Provider ID    10001        1.460299e+06  6.723581e+05  4.559090e+05  7.680036e+05   10005        5.777248e+05  2.004199e+06  3.265536e+06  1.429573e+06   10006        5.246954e+05  1.027439e+06  7.573411e+05  9.105474e+05   10007        3.638231e+06  6.394402e+05  4.694125e+05  7.517467e+05   10008      inf  7.766998e+05  5.401081e+05  9.496953e+05   10011        7.766998e+05inf  1.341775e+06  4.220911e+06   10012        5.401081e+05  1.341775e+06inf  1.119063e+06   10016        9.496953e+05  4.220911e+06  1.119063e+06inf   10018        8.236437e+05  1.242451e+07  1.226941e+06  5.866259e+06   10019        5.372119e+05  1.051748e+06  7.514774e+05  9.362341e+05Provider ID         10018         10019  Provider ID        10001        7.096548e+05  5.436342e+05  10005        1.733554e+06  9.278739e+05  10006        1.020976e+06  2.891822e+07  10007        6.701920e+05  4.638858e+05  10008        8.236437e+05  5.372119e+05  10011        1.242451e+07  1.051748e+06  10012        1.226941e+06  7.514774e+05  10016        5.866259e+06  9.362341e+05  10018      inf  1.048895e+06  10019        1.048895e+06inf[10 rows x 10 columns]


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5645881.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存