3

我的数据如下所示(ch= 通道,det= 检测器):

ch det time counts 
1   1    0    123
    2    0    121
    3    0    125 
2   1    0    212
    2    0    210
    3    0    210 
1   1    1    124
    2    1    125
    3    1    123 
2   1    1    210
    2    1    209
    3    1    213

请注意,实际上,时间列是float12 位左右的有效数字,对于 1 次测量的所有检测器来说仍然是恒定的,但它的值是不可预测的,也不按顺序排列。

我需要创建一个如下所示的数据框:

c  time  mean_counts_over_detectors
1   0       xxx
2   0       yyy
1   1       zzz
1   1       www

即,我想np.mean在每个时间分别应用 1 个通道的检测器的所有计数。我可以编写笨拙的循环,但我觉得 pandas 必须为此内置一些东西。我仍然是 pandas 的初学者,尤其是 MultiIndex 有很多概念,我不确定我应该在文档中寻找什么。

标题包含“条件”,因为我认为也许我想要一个通道的所有检测器的平均值对于时间相同的计数这一事实可以表示为切片条件。

4

2 回答 2

3

与 @meteore 相同,但具有 MultiIndex。

In [55]: df
Out[55]:
             counts
ch det time
1  1   0        123
   2   0        121
   3   0        125
2  1   0        212
   2   0        210
   3   0        210
1  1   1        124
   2   1        125
   3   1        123
2  1   1        210
   2   1        209
   3   1        213

In [56]: df.index
Out[56]:
MultiIndex
[(1L, 1L, 0L) (1L, 2L, 0L) (1L, 3L, 0L) (2L, 1L, 0L) (2L, 2L, 0L)
 (2L, 3L, 0L) (1L, 1L, 1L) (1L, 2L, 1L) (1L, 3L, 1L) (2L, 1L, 1L)
 (2L, 2L, 1L) (2L, 3L, 1L)]

In [57]: df.index.names
Out[57]: ['ch', 'det', 'time']

In [58]: df.groupby(level=['ch', 'time']).mean()
Out[58]:
             counts
ch time
1  0     123.000000
   1     124.000000
2  0     210.666667
   1     210.666667

小心浮点数和 groupby(这是否独立于 MultiIndex),由于与浮点数相关的数字表示/准确性限制,组可能会有所不同。

于 2012-10-29T14:05:50.187 回答
2

不使用 MultiIndexes(如果你有它们,你可以通过 摆脱它们df.reset_index()):

chans = [1,1,1,2,2,2,1,1,1,2,2,2]
df = pd.DataFrame(dict(ch=chans, det=[1,2,3,1,2,3,1,2,3,1,2,3], time=6*[0]+6*[1], counts=np.random.randint(0,500,12)))

使用groupbyandmean作为聚合函数:

>>> df.groupby(['time', 'ch'])['counts'].mean()
time  ch
0     1     315.000000
      2     296.666667
1     1     178.333333
      2     221.666667
Name: counts

其他聚合函数可以通过agg

>>> df.groupby(['time', 'ch'])['counts'].agg(np.ptp)
于 2012-10-29T10:51:48.563 回答