【发布时间】:2022-01-04 00:34:52
【问题描述】:
我正在处理 csv 文件。
我想创建一个连续更新的序列平均值。例如;
我想输出列表中每个单独值的平均值
list; [a, b, c, d, e, f]
formula:
(a)/1= ?
(a+b)/2=?
(a+b+c)/3=?
(a+b+c+d)/4=?
(a+b+c+d+e)/5=?
(a+b+c+d+e+f)/6=?
演示:
如果我有一个清单; [1, 4, 7, 4, 19]
我的输出应该是; [1, 2.5, 4, 4, 7]
解释;
(1)/1=1
(1+4)/2=2.5
(1+4+7)/3=4
(1+4+7+4)/4=4
(1+4+7+4+19)/5=7
就我的python文件而言,它是一个简单的代码:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('somecsvfile.csv')
x = [] #has to be a list of 1 to however many rows are in the "numbers" column, will be a simple [1, 2, 3, 4, 5] etc...
#x will be used to divide the numbers selected in y to give us z
y = df[numbers]
z = #new dataframe derived from the continuous average of y
plt.plot(x, z)
plt.show()
如果需要 numpy 没问题。
【问题讨论】:
-
您的 CSV 文件是什么样的?
-
您要查找的术语是“累积平均值/平均值”。
标签: python pandas csv matplotlib