聚合时间序列数据

我不是数据科学家。我确实知道 python,我目前必须管理定期进入的时间序列数据。这些数据大部分都是零或长时间相同的值,为了节省内存,我想将它们过滤掉。是否有一些标准方法(我显然不知道)或者我应该实现自己的算法?


我想要实现的是以下内容:


interval  value   result

(summed) 

1         0       0

2         0       # removed

3         0       0

4         1       1

5         2       2

6         2       # removed

7         2       # removed

8         2       2

9         0       0

10        0       0

任何帮助表示赞赏!


呼啦一阵风
浏览 143回答 4
4回答

素胚勾勒不出你

谢谢大家!看着答案,我想我可以得出结论,我需要自己动手。我会用你的意见作为灵感。再次感谢 !

呼唤远方

这是代码:l = [0, 0, 0, 1, 2, 2, 2, 2, 0, 0]for (i, ll) in enumerate(l):&nbsp; &nbsp; if i != 0 and ll == l[i-1] and i<len(l)-1 and ll == l[i+1]:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; print(i+1, ll)它产生你想要的东西。您没有指定输入数据的格式,所以我假设它们在列表中。条件ll == l[i-1]和ll == l[i+1]是跳过重复值的关键。

慕无忌1623718

没有快速的函数调用来做你需要的。以下是一种方式import pandas as pddf = pd.DataFrame({'interval':[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'value':[0, 0, 0, 1, 2, 2, 2, 2, 0, 0]}) # example dataframedf['group'] = df['value'].ne(df['value'].shift()).cumsum() # column that increments every time the value changesdf['key'] = 1 # create column of onesdf['key'] =&nbsp; df.groupby('group')['key'].transform('cumsum') # get the cumulative sum&nbsp;df['key'] = df.groupby('group')['key'].transform(lambda x: x.isin( [x.min(), x.max()])) # check which key is minimum and which is maximum by groupdf = df[df['key']==True].drop(columns=['group', 'key']) # keep only relevant casesdf

拉风的咖菲猫

您可以对数据框使用 pandas 查询来实现此目的:import pandas as pdmatrix = [[1,0, 0],[2, 0, 0],[3, 0, 0],[4, 1, 1],[5, 2, 2],[6, 2, 0],[7, 2, 0],[8, 2, 2],[9, 0, 0],[10,0, 0]]df = pd.DataFrame(matrix, columns=list('abc'))print(df.query("c != 0"))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python