2017-05-31 125 views
-3

我正在将多个数据文件读入数据框并计算平均值。在我连接数据框后,我再次计算平均值,但熊猫给我的错误答案。熊猫给出了错误的意思

temp = pd.read_csv(appDelayFile, delimiter='\t') 
temp = temp.groupby(['Type', 'Node']).mean() 
temp = temp.ix['FullDelay'] 

d = pd.concat([d, temp]) 
print d # separate parsed data frames 
d = d.groupby(d.index).mean() 
print d # after calculating the mean 

在第一次印刷我得到('0.574193', '0.441335', and '2.71299'),其平均值为'1.2428393333'。但第二次印刷给我'1.610377'

代码有问题吗?或者这是一个错误?

**编辑**

样本数据文件1:

Time  Node AppId SeqNo Type  DelayS  RetxCount HopCount 
0.054701 25 1 0 LastDelay 0.054701 1 8 
0.054701 25 1 0 FullDelay 0.054701 1 8 
0.00708243 26 1 0 LastDelay 0.00708243 1 2 
0.00708243 26 1 0 FullDelay 0.00708243 1 2 
0.036943 25 1 0 LastDelay 0.036943 1 6 
0.036943 25 1 0 FullDelay 0.036943 1 6 
0.0582151 26 1 0 LastDelay 0.0582151 1 12 
0.0582151 26 1 0 FullDelay 0.0582151 1 12 

样本数据文件2:

Time  Node AppId SeqNo Type  DelayS  RetxCount HopCount 
0.0252673 25 1 0 LastDelay 0.0252673 1 6 
0.0252673 25 1 0 FullDelay 0.0252673 1 6 
0.00655327 26 1 0 LastDelay 0.00655327 1 2 
0.00655327 26 1 0 FullDelay 0.00655327 1 2 
0.023523 25 1 0 LastDelay 0.023523 1 8 
0.023523 25 1 0 FullDelay 0.023523 1 8 
0.0380394 26 1 0 LastDelay 0.0380394 1 4 
0.0380394 26 1 0 FullDelay 0.0380394 1 4 

样本数据文件3:

Time  Node AppId SeqNo Type  DelayS  RetxCount HopCount 
0.0276086 25 1 0 LastDelay 0.0276086 1 8 
0.0276086 25 1 0 FullDelay 0.0276086 1 8 
0.0197642 26 1 0 LastDelay 0.0197642 1 4 
0.0197642 26 1 0 FullDelay 0.0197642 1 4 
0.00708267 25 1 0 LastDelay 0.00708267 1 2 
0.00708267 25 1 0 FullDelay 0.00708267 1 2 
0.00708268 26 1 0 LastDelay 0.00708268 1 2 
0.00708268 26 1 0 FullDelay 0.00708268 1 2 

已析数据文件:

   Time AppId  SeqNo DelayS  DelayUS RetxCount HopCount 
25 0.045822  1  0 0.045822 45822.000   1   7 
26 0.032649  1  0 0.032649 32648.765   1   7 
      Time AppId SeqNo DelayS DelayUS RetxCount HopCount 
Node                 
25 0.024395  1  0 0.024395 24395.150   1   7 
26 0.022296  1  0 0.022296 22296.335   1   3 
      Time AppId SeqNo DelayS DelayUS RetxCount HopCount 
Node                 
25 0.017346  1  0 0.017346 17345.635   1   5 
26 0.013423  1  0 0.013423 13423.440   1   3 

第二打印示出了数据帧的平均(这是错误的):

  Time AppId  SeqNo DelayS  DelayUS RetxCount HopCount 
25 0.026227  1  0 0.026227 26227.105   1   6 
26 0.020448  1  0 0.020448 20447.995   1   4 

这是print temp = temp.groupby(['Type', 'Node']).count()

  Time AppId  SeqNo DelayS  DelayUS RetxCount HopCount 
Node               
25  2  2  2  2  2   2   2 
26  2  2  2  2  2   2   2 
+3

你可以发布你使用的数据吗?什么是'd'? – darthbith

+3

对于调试问题,您需要提供[mcve]。请包括一些显示问题的数据集(尽可能简短,并在最佳情况下复制和粘贴)。 :) – MSeifert

+0

你可以显示'print'语句的_actual_输出吗?第二个打印语句应该显示一个DataFrame(或可能是一个Series),而不是一个单独的值。 –

回答

0

的输出。如果组大小是不是所有的相等,你不应该期望整体意思是mean of the group means

temp.groupby(['Type', 'Node']).count() 

您将看到群体有不同的尺寸。

如果你想的手段来搭配,你可以做一个加权平均,如以下

from __future__ import print_function, division 
import pandas as pd 
import numpy as np 
np.random.seed(10) 
df = pd.DataFrame(np.random.randint(0, 3, size=(20,2)), 
        columns=['node', 'type']) 
df['delay'] = np.random.uniform(size=20) 
grouper = df.groupby(['type', 'node']).delay 
print(df.delay.mean()) 
# 0.512131169932 
print(grouper.mean().mean()) 
# 0.55613710694900131 
print ((grouper.mean() * grouper.count()).sum()/df.index.size) 
# 0.51213116993222196 
+0

谢谢。尺寸不同的唯一时间是将原始数据文件转换为熊猫数据框。 – John

+0

@john你是说你可以做一个temp.groupby(['Type','Node'])。count()。unique()并且只看到一个值? –

+0

嗨@DavidNehme,我按照你的要求添加了.count的输出。我想说的是每个节点(25到40)的平均值是正确的。问题是当我连接10个数据帧并计算平均值时。 – John

0

明白了!问题在于,我每次连接数据帧时都计算平均值,而不是连接所有数据框并计算最后的平均值。即,我将d = d.groupby(d.index).mean()移至第一个for循环,而不是在嵌套循环中。

+1

所以有一个for循环?!但是,在问题中显示的代码中,没有可看到的循环!如果您在原始问题中显示了您使用的实际代码,那么您可能会在几分钟内得到答案。 –