【问题标题】:is there a way to groupby bulks using python pandas?有没有办法使用 python pandas 进行分组?
【发布时间】:2021-09-21 09:18:51
【问题描述】:

我有一个实验的不同 Trials 的不同时间序列的 DataFrame。数据包含车辆加速状态的列。状态为-1减速,0刹车,1加速。

我想对每个 -1,0,1 做一个总结,并且需要一种方法来分组所有相邻的 1 而忽略其他 1。 0 和 -1 也一样。

例如: 这就是我所拥有的:

X axis acceleration X axis state
0 0
0 0
0 0
0.87 1
0.88 1
0 0
-0.28 -1
-0.27 -1
0 0
0 0
0.46 1
0.23 1

这是我想要的:

mean X axis acceleration X axis state event number
0 0 1
0.875 1 2
0 0 3
-0.275 -1 4
0 0 5
0.345 1 6

【问题讨论】:

    标签: python pandas time-series pandas-groupby


    【解决方案1】:

    X axis state 列的连续值创建组并聚合mean,按list 列的最后更改顺序:

    g = df['X axis state'].ne(df['X axis state'].shift()).cumsum().rename('event number')
    
    df = df.groupby([g, 'X axis state'])['X axis acceleration'].mean().reset_index()
    df = df[['X axis acceleration','X axis state','event number']]
    
    print (df)
       X axis acceleration  X axis state  event number
    0                0.000             0             1
    1                0.875             1             2
    2                0.000             0             3
    3               -0.275            -1             4
    4                0.000             0             5
    5                0.345             1             6
    

    编辑:如果按移位值进行比较,如果值发生更改,则得到 Trues:

    print (df['X axis state'].ne(df['X axis state'].shift()))
    0      True
    1     False
    2     False
    3      True
    4     False
    5      True
    6      True
    7     False
    8      True
    9     False
    10     True
    11    False
    Name: X axis state, dtype: bool
    

    所以如果添加累积和得到组:

    print (df['X axis state'].ne(df['X axis state'].shift()).cumsum().rename('event number'))
    0     1
    1     1
    2     1
    3     2
    4     2
    5     3
    6     4
    7     4
    8     5
    9     5
    10    6
    11    6
    Name: event number, dtype: int32
    

    【讨论】:

    • 哦,太好了!,您有没有机会描述第一行的作用?
    • @MichaelFleicherTal - 添加到答案中。
    猜你喜欢
    • 2011-02-26
    • 1970-01-01
    • 2021-06-14
    • 2017-10-17
    • 2019-03-04
    • 2015-11-29
    • 2016-12-02
    • 2021-01-14
    • 2021-05-16
    相关资源
    最近更新 更多