[英]Find cumulative sum of a list while reading data from csv file
I want to find cumulative sum of the list
我想找到cumulative sum of the list
I am reading 30 similar csv files located in the folder deg_pvsyst_runs.我正在读取 deg_pvsyst_runs 文件夹中的 30 个类似的 csv 文件。 Files are named as:文件命名为:
"Energy_Base_Year00_-0.6%modqual.csv", "Energy_Base_Year01_-0.3%modqual.csv", ......, "Energy_Base_Year30_-8.4%modqual.csv
" ”
I am reading E_Grid Column (After skipping rows 0-9, 11 and 12 which has header info) in the each of the above csv files.我正在阅读上述每个 csv 文件中的 E_Grid 列(在跳过具有标题信息的第 0-9、11 和 12 行之后)。 Please suggest请建议
a) If there is a shorter/easier way to read csv while skipping rows as opposed to what I have done ( skiprows=[0,1,2,3,4,5,6,7,8,9,11,12]
) a)如果在跳过行时有更短/更简单的方法来读取 csv,而不是我所做的( skiprows=[0,1,2,3,4,5,6,7,8,9,11,12]
)
b) If using itertools
method or cumsum
function would be a better option to calculate the cumulative sum of list called Cumulative_Annual_Deg
b) 如果使用itertools
方法或cumsum
函数将是计算名为Cumulative_Annual_Deg
的列表的累积总和的更好选择
CODE:代码:
import os, csv, re
import pandas as pd
from itertools import accumulate
Year, Degradation, Mean_EP, Annual_Deg, Cumulative_Annual_Deg =[],[],[],[],[]
cwd = os.getcwd()
csv_files = [f for f in os.listdir(cwd + '\\' + 'deg_pvsyst_runs') if f.endswith('.csv')]
for i,j in enumerate(csv_files):
df = pd.read_csv(os.getcwd() + "\\deg_pvsyst_runs\\" + j, skiprows=[0,1,2,3,4,5,6,7,8,9,11,12])
Mean_EP.append(df['E_Grid'].sum()/10**6)
Annual_Deg.append((Mean_EP[i-1] - Mean_EP[i])/Mean_EP[i-1])
Cumulative_Annual_Deg.append(list(accumulate(Annual_Deg[i])))
ERROR:错误:
Cumulative_Annual_Deg.append(list(accumulate(Annual_Deg[i])))
TypeError: 'numpy.float64' object is not iterable
EDIT:编辑:
Annual_Deg
is list consisting of delta (% change) calculate between the csv files. Annual_Deg
是由 csv 文件之间的增量(变化百分比)计算组成的列表。 I want to calculate Cumulative_Annual_Deg
to calculate cumulative sum.我想计算Cumulative_Annual_Deg
来计算累积总和。
Desired output :所需的输出:
Annual_Mean_EP = [9559.88, 9533.31, 9506.67,...,8731.85]
Annual_Deg = [0, 0.00278, 0.00279,...,0.00297]
Cumulative_Annual_Deg' = [0, 0.00278, 0.00557, ..., 0.08661]
OR Screenshot或截图
The cumulative sum could be calculated after the list is calculated.可以在计算列表后计算累积和。 So the accumulate line should be brought outside the for loop.所以累加行应该放在 for 循环之外。 Replace following line替换以下行
Cumulative_Annual_Deg.append(list(accumulate(Annual_Deg[i])))
with和
Cumulative_Annual_Deg = list(accumulate(Annual_Deg))
You can find more info about itertools on https://docs.python.org/3/library/itertools.html#itertool-functions您可以在https://docs.python.org/3/library/itertools.html#itertool-functions上找到有关 itertools 的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.