我对python很新。
如何在pandas中使用dataframe进行多次迭代
我有8760个太阳天顶值的值----> theta_z
太阳方位角值的8760值----> theta_a
现在我必须迭代两个条件
1)面板倾斜(0-90)---> theta_t
2)面板方位角(0-360)-----> theta_azi
我必须执行以下计算
x=arccos(cos(theta_z)*cos(theta_t)+sin(theta_z)*sin(theta_t)*cos(theta_a-theta_azi))
for (i=0,i<8761,i=i+1)
for (j=0,j<361,j=j+1)
for (k=0,k<91,k=k+1)
x=....
如果我将它们作为每个的单独数据帧,我如何在pandas中进行迭代而不是使用下面的循环。我需要获得8760 x 361 x 91值或361 x 91组8760值。
答案 0 :(得分:0)
假设你有一个这样的数据框:
import pandas as pd
df = pd.DataFrame({
'zenith': [0.0, 15.0, 30.0],
'azimuth': [0.0, 10.0, 20.0]
})
print(df)
# zenith azimuth
# 0 0.0 0.0
# 1 15.0 10.0
# 2 30.0 20.0
你可以
import numpy as np
然后,对于 theta_z
和 theta_a
(以弧度为单位)的任何给定值,例如
# Data from 2nd row of the dataframe
theta_z, theta_a = np.deg2rad(df.iloc[1])
可以使用 numpy.meshgrid
而不是循环遍历 tilt
和 azimuth
值并按如下方式计算 x
值:
import numpy as np
# Take one of the rows of the dataframe
theta_z, theta_a = np.deg2rad(df.iloc[1])
# Create the desired ranges for tilt and azimuth
range_theta_t_deg = np.arange(0.0, 91.0, 1.0)
range_theta_t = np.deg2rad(range_theta_t_deg)
range_theta_azi_deg = np.arange(0.0, 361.0, 1.0)
range_theta_azi = np.deg2rad(range_theta_azi_deg)
# Create a grid of tilt and azimuth values with the specified ranges
theta_t, theta_azi = np.meshgrid(range_theta_t, range_theta_azi, sparse=True)
# Compute x for each point in the grid, and convert back to degrees if needed
x_rad = np.arccos(np.cos(theta_z) * np.cos(theta_t) +
np.sin(theta_z) * np.sin(theta_t) * np.cos(theta_a - theta_azi))
x = np.rad2deg(x_rad)
这会产生一个形状为 (361, 91)
的数组,同样的想法可以应用于原始数据帧的 8760 行中的任何一行。
print(x)
# [[15. 14.0162433 13.03490594 ... 73.23529277 74.23419621 75.23311039]
# [15. 14.01316484 13.02829452 ... 73.19070162 74.18981137 75.18892988]
# [15. 14.01040734 13.02237082 ... 73.15075884 74.150054 75.1493561]
# ...
# [15. 14.02335841 13.05017949 ... 73.33835117 74.33678044 75.335225]
# [15. 14.01964157 13.04220205 ... 73.28451547 74.28319197 75.2818814]
# [15. 14.0162433 13.03490594 ... 73.23529277 74.23419621 75.23311039]]