从pandas数据帧计算平均值

时间:2018-05-16 09:18:38

标签: python pandas dataframe mean pandas-groupby

我正在尝试找到一种从数据框中的值计算平均值的好方法。它包含来自实验的测量数据,并从Excel工作表导入。列包含经过的时间,电流和相应的电压。

电流逐步改变,然后保持一段时间(当前值略有不同,因此它们对于每一步都不完全相同)。现在我想计算每个当前步骤的平均电压。由于在一步之后电压稳定后需要一些时间,我还想在一步之后省去前几个电压值。

目前我正在使用循环进行此操作,但我想知道使用groupby函数(或其他可能)有更好的方法。

如果您需要更多详细信息或说明,请说明。

数据示例:

              s        [A]       [V]
0    303.000000  -0.000982  0.857416
1    636.000000   0.879220  0.792504
2    699.000000   1.759356  0.752446
3    759.000000   3.519479  0.707161
4    816.000000   5.278372  0.669020
5    876.000000   7.064800  0.637848
6    939.000000   8.828799  0.611196
7    999.000000  10.593054  0.584402
8   1115.333333  12.357359  0.556127
9   1352.000000  14.117167  0.528826
10  1382.000000  15.882287  0.498577
11  1439.000000  17.646748  0.468379
12  1502.000000  19.410817  0.437342
13  1562.666667  21.175572  0.402381
14  1621.000000  22.939826  0.365724
15  1681.000000  24.704600  0.317134
16  1744.000000  26.468235  0.256047
17  1807.000000  28.231037  0.179606
18  1861.000000   8.819844  0.638190

输出:

df = df[['s','[A]','[V]']]

#Looping over the rows to separate current points
b=df['[A]'].iloc[0]
start=0
list = []
for index, row in df.iterrows():
    if not math.isclose(row['[A]'], b, abs_tol=1e-02):
        b=row['[A]']
        list.append(df.iloc[start:index])
        start=index
list.append(df.iloc[start:])

#Deleting first few points after each current change
list_b = []
for l in list:
    list_b.append(l.iloc[3:])

#Calculating mean values for each current point
list_c = []
for l in list_b:
    list_c.append(l.mean())

result=pd.DataFrame(list_c)

目前的做法:

this.data.title = '';
this.data.paragraph = [{}] instead of '';

1 个答案:

答案 0 :(得分:0)

Does this help?

df.groupby(['Columnname', 'Columnname2']).mean()

您可能需要为每个步骤创建中间数据帧。你能提供一个你想要的输出的例子吗?