我刚刚开始研究Python。 在我的CSV文件中,汇总了用于计算的数据。此类数据的一部分如下:
months,t_mid,hours,N,N-E,E,S-E,S,S-W,W,N-W,horizontal
January,-4.6,744,13,13,19,31,40,33,20,13,29
February,-3.4,672,21,23,33,52,64,54,35,23,55
March,0.7,744,32,38,54,72,83,75,57,38,96
April,8,720,38,50,71,84,84,81,68,49,138
May,13.8,744,52,71,92,96,89,94,87,69,186
June,16.7,720,61,79,98,97,87,95,93,78,203
July,18.2,744,59,76,95,96,87,94,92,74,192
August,17.5,744,43,62,87,100,98,98,83,60,174
September,13.1,720,29,38,60,77,86,77,57,38,113
October,7.7,744,18,21,35,56,68,55,35,21,66
November,2.1,720,10,10,15,26,33,27,15,10,28
December,-2.6,744,9,9,12,22,28,22,13,9,20
U_wall,A_wall,U_window,A_window-被世界各地收集: N,N-E,E,S-E,S,S-W,W,N-W。这将是数字值。
I_sol-世界将会改变它,数据必须从CSV文件中读取。
世界各地的计算公式:
QH_sol = (0.0301 * U_wall * A_wall + 0.4725 * A_window * I_sol - 0.2052868010 * U_window * A_window * ((t_mid + 273) ^ 3)) * hours
我不知道如何执行以下操作:
如何获取给定公式的简单代码。 我唯一的想法是在世界各地收到复制后的SUM
需要获取QH_sol的表SUM(世界各地),具体取决于月(在给定所有变量的情况下其变化方式)。像这样的东西:
months | QH_sol
January -235914
February 54646
.....
December 4656
如果您能帮助我编写用于形成表格的代码,我将不胜感激
当I_sol等于1时,我得到错误:
TypeError:无法将序列乘以'float'类型的非整数
import pandas as pd
import numpy as np
pd.options.display.float_format = '{:,.2f}'.format
temp_df = pd.read_csv("d:\city.csv")
t_mid=temp_df.loc[lambda df: df.months == months, :]["temp"].to_string(index=False)
print(t_mid)#it for output test
hours=temp_df.loc[lambda df: df.months == months, :]["hours"].to_string(index=False)
print(hours) #it for output test
QH_sol = (0.0301 * U_wall * A_wall + 0.4725 * A_window * I_sol - 0.2052868010 * U_window * A_window * ((t_mid + 273) ^ 3)) * hours
现在我正在尝试下一个代码:
import pandas as pd
import numpy as np
pd.options.display.float_format = '{:,.2f}'.format
temp_df = pd.read_csv("d:\city.csv")
U_wall = float(U_Wall)
A_wall = float(A_Wall)
U_window = float(U_Window)
A_window = float(A_Window)
#t_mid=temp_df.loc[lambda df: df.months == months, :]["temp"].to_string(index=False)
T_mid=temp_df.loc[lambda df: df.months == months, :]["temp"]
t_mid = pd.to_numeric(pd.Series(T_mid))
print(t_mid)
#hours=temp_df.loc[lambda df: df.months == months, :]["hours"].to_string(index=False)
Hours=temp_df.loc[lambda df: df.months == months, :]["hours"]
hours = pd.to_numeric(pd.Series(Hours))
print(hours)
i_sol=temp_df.loc[lambda df: df.months == months, :]["N"]
I_sol = pd.to_numeric(pd.Series(i_sol))
print (I_sol)
temp_df["QH_sol"] = (0.0301 * temp_df["U_wall"] * temp_df["A_wall"] + 0,4725 * temp_df["A_window"] * temp_df["I_sol"] - 0.2052868010 * temp_df["U_window"] * temp_df["A_window"] * ((temp_df["t_mid"] + 273) ^ 3)) * temp_df["hours"]
并接收下一个输出:
0 -4.60
1 -3.40
2 0.70
3 8.00
4 13.80
5 16.70
6 18.20
7 17.50
8 13.10
9 7.70
10 2.10
11 -2.60
Name: temp, dtype: float64
0 744.00
1 672.00
2 744.00
3 720.00
4 744.00
5 720.00
6 744.00
7 744.00
8 720.00
9 744.00
10 720.00
11 744.00
Name: hours, dtype: float64
0 13.00
1 21.00
2 32.00
3 38.00
4 52.00
5 61.00
6 59.00
7 43.00
8 29.00
9 18.00
10 10.00
11 9.00
Name: N, dtype: float64
Traceback (most recent call last):
File "C:\Users\Hirol\Anaconda3\envs\GH-3.6+R\lib\site-packages\pandas\core\indexes\base.py", line 2657, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'U_wall'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\GH_CPython\PythonFileWritten_23.py", line 36, in <module>
temp_df["QH_sol"] = (0.0301 * temp_df["U_wall"] * temp_df["A_wall"] + 0,4725 * temp_df["A_window"] * temp_df["I_sol"] - 0.2052868010*(10^-7) * temp_df["U_window"] * temp_df["A_window"] * ((temp_df["t_mid"] + 273) ^ 3)) * temp_df["hours"]
File "C:\Users\Hirol\Anaconda3\envs\GH-3.6+R\lib\site-packages\pandas\core\frame.py", line 2927, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\Hirol\Anaconda3\envs\GH-3.6+R\lib\site-packages\pandas\core\indexes\base.py", line 2659, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'U_wall'