python - 将数据帧按特定列压缩为包含第一个和最后一个时间戳以及值平均值的行

标签 python pandas

我已经标记了按时间戳排序的数据组,我想将其减少到开始时间戳和最后时间戳,并获取与该组中该标记相对应的值的平均值。起始数据框示例:

          timestamp          value      mark
1   2016-11-07 11:00:00       0.781726     1  
2   2016-11-07 11:03:00       0.812757     2  
3   2016-11-07 11:05:00       0.845348     2  
4   2016-11-07 11:07:00       0.817394     2  
5   2016-11-07 11:11:00       0.760787     1  
6   2016-11-07 11:13:00       0.807892     1 
7   2016-11-07 11:15:00       0.812965     1  
8   2016-11-07 11:18:00       0.822001     1 

我想要实现的目标:

        start_timestamp         end_timestamp  (mean_)value  mark  
1   2016-11-07 11:00:00   2016-11-07 11:00:00     0.781726      1
2   2016-11-07 11:03:00   2016-11-07 11:07:00     0.825166      2
3   2016-11-07 11:11:00   2016-11-07 11:18:00     0.800911      1

知道执行此操作的最佳方法吗?我应该首先用唯一的标记标记每个批处理吗?

最佳答案

您需要groupby由重复列标记中的唯一系列组成,然后aggregate first , lastmean :

print ((df.mark != df.mark.shift()).cumsum())
1    1
2    2
3    2
4    2
5    3
6    3
7    3
8    3
Name: mark, dtype: int32

df1 = df.groupby((df.mark != df.mark.shift()).cumsum()) \
         .agg({'timestamp': ['first','last'], 'value':'mean', 'mark': 'first'})

#reset MultiIndex in columns
df1.columns = ['_'.join(col) for col in df1.columns]
#if necessary rename columns
df1 = df1.rename(columns=({'timestamp_first':'start_timestamp', 
                           'timestamp_last':'end_timestamp',
                           'mark_first':'mark','value_mean':'(mean_)value'})) \
          .rename_axis(None)

print (df1)         
      start_timestamp       end_timestamp  mark  (mean_)value
1 2016-11-07 11:00:00 2016-11-07 11:00:00     1      0.781726
2 2016-11-07 11:03:00 2016-11-07 11:07:00     2      0.825166
3 2016-11-07 11:11:00 2016-11-07 11:18:00     1      0.800911

关于python - 将数据帧按特定列压缩为包含第一个和最后一个时间戳以及值平均值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40508309/

相关文章:

Python if 语句的结果放入pandas dataframe中

python - 有效地获得 3 个不同大小和类型的 numpy 数组的排列

python - 为什么我的简单Spark应用程序运行这么慢?

python - Numpy复杂的数据结构

python - 使用 Sqlite 查询将 pandas 数据框中的数据插入到 Sqlite

python - 在 Pandas 系列中扩展列表

python - 从文本文件 Pandas 中读取列

python - 如何在 Pandas 数据框中按行值对日期时间列进行排序?

Python 计数元音

python - 如何使用网络爬虫从 URL 中获取正确的 Python 源代码?