【发布时间】:2020-05-13 00:07:46
【问题描述】:
the image shows the test dataset I am using to verify if the right averages are being calculated.
我希望能够根据“T”列中的过滤值获得“G”列中相应值的平均值。
所以我设置了“T”列的值,我想根据这些值对“G”列中的值求和,然后将总数除以计数以获得平均值,该平均值附加到变量中。 但是平均值没有正确计算。见下文 screenshot
total=0
g_avg=[]
output=[]
counter=0
for i, row in df_new.iterrows():
if (row['T'] > 2):
counter+=1
total+=row['G']
if (counter != 0 and row['T']==10):
g_avg.append(total/counter)
counter = 0
total = 0
print(g_avg)
下面是一组更好的数据,因为“T”值存在重复,所以当 T 值在一定范围内(即从凌晨 2 点到 10 点)时,我需要一个计数器来获得 G 值的平均值上午等 sorry it wont allow me to just paste the dataset so ive took a snippy of it
【问题讨论】:
-
欢迎使用 stackoverflow。请提供包含数据的minimal reproducible example。列
"T"似乎没有任何值10,因此您甚至永远不会输入第二个if。您也可以只使用df_new[df_new['T'] > 2]['G'].mean() -
@sim 它不允许我粘贴数据集,但我已经上传了代码。
-
@sim 基本上该程序的要点是数据集但包含 1 周的数据,我们希望在“T”列在例如 2 之间时取“g”列的平均值-7pm 并将其附加到列表中
标签: python pandas jupyter-notebook