处理Pandas中的复制数据

时间:2017-09-12 23:43:28

标签: python pandas

我试图绘制一些数据随时间的散点图,其中std偏差为误差条。对于两个不同的样品A和B,我对每个时间点都进行了三次测量。

Date A1 A2 A3 B1 B2 B3
1/1/17 4 5 6 2 3 4
1/2/17 6 7 8 5 6 4

所以我输入的数据如下:

import pandas as pd
columns = ['Date', 'A1', 'A2', 'A3', 'B1', 'B2', 'B3']
dat = pd.read_csv('data', sep='\t', names=columns)

那么如何组合复制列以便我可以计算标准偏差然后绘制?我正在考虑重命名A和B列,使它们相同,然后可以合并?

1 个答案:

答案 0 :(得分:3)

我必须承认,我并没有真正得到你想要的东西,但我认为这很酷。

  • 转置,因为我想运行groupby / agg但尚未在列上实施
  • 按列名的第一个字符分组,然后找到meanstd
  • 用它来绘制带错误条的东西。
d = df.T.groupby(lambda x: x[0]).agg(['mean', 'std']).T
d.xs('mean', level=1).plot.bar(yerr=d.xs('std', level=1))

或者路径略有不同的想法

d = df.T.groupby(lambda x: x[0]).agg(['mean', 'std']).stack(0).unstack(0)
d['mean'].plot.bar(yerr=d['std'])

enter image description here

否则,我们可以做一次减少

df.groupby(lambda x: x[0], 1).mean()

        A  B
Date        
1/1/17  5  3
1/2/17  7  5