熊猫能否通过在Python 2.7中使用熊猫来选择某些数据或所有特定于数据的列并每5行取平均值?

时间:2018-09-10 08:09:58

标签: python python-2.7 pandas dataframe

我想计算特定列中每5行的平均值或std值,并通过在python 2.7中使用pandas从另一列中选择数据(我选择的第一个或最后一个或所有或一些数据)

这是我的数据框(示例):

>>df
       DateTime     Product     Location       Value       Place
0    12-07-2018           A           S1       1.313       601
1    12-07-2018           B           S1       3.089       601/14
2    12-07-2018           C           S1       1.890       601
3    12-07-2018           D           S1       3.136       601
4**  12-07-2018           E           S1       3.258       601/15
5    13-07-2018           F           S1       3.113       601
6    13-07-2018           G           S1       2.651       601/12
7    13-07-2018           H           S1       2.135       601
8    13-07-2018           I           S1       1.555       602
9**  14-07-2018           J           S1       2.009       602
10   14-07-2018           K           S1       1.757       602
11   14-07-2018           L           S1       1.808       602/11
12   14-07-2018           M           S1       1.511       603/10
13   15-07-2018           N           S1       2.265       603
14** 15-07-2018           O           S1       2.356       603
15   15-07-2018           P           S1       2.950       603/09
16   15-07-2018           Q           S1       3.300       603/09

现在,我可以每5行取平均值,并使用此代码从示例。取平均值并选择最后):

new_df = df[:(len(df)//5)*5].groupby(df[:(len(df)//5)*5].index // 5).agg({'DateTime':'last', 'Value':'mean', 'Place':'last'})

这是每5行平均值的结果,然后选择最后一个数据(示例。平均值,然后选择最后):

>> new_df
     DateTime       Value     Place
0  12-07-2018      2.5372     601/15
1  14-07-2018      2.2926     602
2  15-07-2018      1.9394     603

如果我想要相同的结果(平均值,请选择“最后”和“全部”):

>> new_df
     DateTime   Product      Value      Place
0  12-07-2018   A,B,C,D,E    2.5372     601, 601/14, 601, 601, 601/15
1  14-07-2018   F,G,H,I,J    2.2926     601, 601/12, 601, 602, 602 
2  15-07-2018   K,L,M,N,O    1.9394     602, 602, 602/11, 603/10, 603

和(平均,选择一些数据)

>> new_df
     DateTime   Product      Value      Place
0  12-07-2018   A,C,E        2.5372     601, 601/14, 601
1  14-07-2018   F,G          2.2926     601, 601/12
2  15-07-2018   L,M,N,O      1.9394     602/11

熊猫可以这样做吗?

注意:每5行添加**以便于观察。

2 个答案:

答案 0 :(得分:2)

在聚合字典中使用.join

d = {'DateTime':'last', 'Value':'mean', 'Place':', '.join, 'Product':', '.join}
new_df = df[:(len(df)//5)*5].groupby(df[:(len(df)//5)*5].index // 5).agg(d)
print (new_df)
    DateTime   Value                          Place        Product
0 2018-12-07  2.5372  601, 601/14, 601, 601, 601/15  A, B, C, D, E
1 2018-07-14  2.2926     601, 601/12, 601, 602, 602  F, G, H, I, J
2 2018-07-15  1.9394  602, 602/11, 603/10, 603, 603  K, L, M, N, O

但是如果要在聚合函数中进行过滤:

def f(x):
    a = x['DateTime'].iloc[-1]
    b = x['Value'].mean()

    x = x[x['Product'].isin(['A','C','E','F','G','L','M','N', 'O'])]
    c = ', '.join(x['Place'])
    d = ', '.join(x['Product'])
    return pd.Series([a,b,c,d], index=['DateTime','Value','Place','Product'])

new_df = df[:(len(df)//5)*5].groupby(df[:(len(df)//5)*5].index // 5).apply(f)
print (new_df)
    DateTime   Value                     Place     Product
0 2018-12-07  2.5372          601, 601, 601/15     A, C, E
1 2018-07-14  2.2926               601, 601/12        F, G
2 2018-07-15  1.9394  602/11, 603/10, 603, 603  L, M, N, O

答案 1 :(得分:0)

enter image description here

错误原因可能是因为我的“位置”列中的信息是这样的。