python - Lists/DataFrames - 对 Python 中的所有值运行一个函数
问题描述
我现在被卡住了,真的不知道如何解决这个问题。我想将此计算应用于列表/数据框:
方程本身对我来说并不是真正的问题,我可以很容易地手动解决它,但这与我拥有的数据量无关。
- v :要近似的值
- vi:已知值(在我的情况下是温度)
- di:到近似点的距离
所以基本上这是为了计算/近似距离正方形角落一定距离的位置的新温度值:
import pandas as pd
import numpy as np
import xarray as xr
import math
filepath = r'F:\Data\data.nc' # just the path to the file
obj= xr.open_dataset(filepath)
# This is where I get the coordinates for each of the corners of the square
# from the netcdf4 file
lat = 9.7398
lon = 51.2695
xlat = obj['XLAT'].values
xlon = obj['XLON'].values
p_1 = [xlat[0,0], xlon[0,0]]
p_2 = [xlat[0,1], xlon[0,1]]
p_3 = [xlat[1,0], xlon[1,0]]
p_4 = [xlat[1,1], xlon[1,1]]
p_rect = [p_1, p_2, p_3, p_4]
p_orig = [lat, lon]
#=================================================
# Calculates the distance between the points
# d = sqrt((x2-x1)^2 + (y2-y1)^2))
#=================================================
distance = []
for coord in p_rect:
distance.append(math.sqrt(math.pow(coord[0]-p_orig[0],2)+math.pow(coord[1]-p_orig[1],2)))
# to get the values for they key['WS'] for example:
a = obj['WS'].values[:,0,0,0] # Array of floats for the first values
b = obj['WS'].values[:,0,0,1] # Array of floats for the second values
c = obj['WS'].values[:,0,1,0] # Array of floats for the third values
d = obj['WS'].values[:,0,1,1] # Array of floats for the fourth values
从那时起,我不知道我应该如何继续,我应该怎么做:
df = pd.DataFrame()
df['a'] = a
df['b'] = b
df['c'] = c
df['d'] = d
然后以某种方式使用 DataFrames,并在我获得所需的值后删除 abcd 或者我应该先使用列表,然后只将结果添加到数据帧中。我有点失落。
到目前为止,我唯一想到的就是如果我手动执行它会是什么样子:
for i starting at 0 and ending if the end of the list [a, b, c d have the same length] is reached .
1/a[i]^2*distance[0] + 1/b[i]^2*distance[1] + 1/c[i]^2*distance[2] + 1/d[i]^2*distance[3]
v = ------------------------------------------------------------------------------------------
1/a[i]^2 + 1/b[i]^2 + 1/c[i]^2 + 1/d[i]^2
'''
这是我第一次对列表/数据框进行如此复杂的计算(至少对我而言)。我希望你能帮助我解决这个问题,或者至少把我推向正确的方向。
PS:这里是文件的链接: LINK TO FILE
解决方案
只需矢量化您的计算。使用数据框,您可以直接在列上运行整个算术运算,就好像它们是标量以生成另一列一样,df['v']
. 下面假设距离是四个标量的列表,记住在 Python^
中并不意味着力量,而是我们**
。
df = pd.DataFrame({'a':a, 'b':b, 'c':c, 'd':d})
df['v'] = (1/df['a']**2 * distance[0] +
1/df['b']**2 * distance[1] +
1/df['c']**2 * distance[2] +
1/df['d']**2 * distance[3]) / (1/df['a']**2 +
1/df['b']**2 +
1/df['c']**2 +
1/df['d']**2)
或者使用 Pandas 系列二元运算符的函数形式。以下遵循操作顺序(括号->指数->乘法/除法->加法/减法):
df['v'] = (df['a'].pow(2).pow(-1).mul(distance[0]) +
df['b'].pow(2).pow(-1).mul(distance[1]) +
df['c'].pow(2).pow(-1).mul(distance[2]) +
df['d'].pow(2).pow(-1).mul(distance[3])) / (df['a'].pow(2).pow(-1) +
df['b'].pow(2).pow(-1) +
df['c'].pow(2).pow(-1) +
df['d'].pow(2).pow(-1))
推荐阅读
- python - 如何在类似于点积的numpy中获得“点加法”?
- model - 如何在 Odoo 13 企业版中使用相同的 MODEL 在两个菜单中显示不同的选择字段?
- html - 动画对齐
- angular - NativeScript Angular 错误 TS5060:如果不指定“--baseUrl”选项,则无法使用选项“路径”
- python - Leetcode 17关于python字典关键问题的问题
- reactjs - 如何在 React js 中使用 graphql api 检查和更新单选按钮的值?
- c# - “至少一个签名无效”
- ruby-on-rails - Rails:ActiveJob:重试相同的工作有一些延迟
- unity3d - 统一缩放脚本
- sql - 从 AWS Athena 中的表中选择每组 1000 个样本