- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
示例数据框:
Date | ID | Type 1 | Type 2 | Type 3
-----------------------------------------
2017-06-05 | 1 | 2 | 1 | 0
2017-08-05 | 1 | 0 | 1 | 0
2017-10-05 | 1 | 2 | 1 | 1
2017-06-05 | 2 | 0 | 1 | 0
2017-07-05 | 2 | 2 | 0 | 0
2017-09-15 | 3 | 0 | 0 | 5
我想按月分组,以便每个 ID 每月都有行,直到最后一个可用数据。例如,在本例中,ID=1 包含第 6 个月到第 10 个月的数据。因此,ID=1 从第 6 个月到第 10 个月每月获取行。
ID=1 的预期输出:
Date | ID | Type 1 | Type 2 | Type 3
-----------------------------------------
2017-06-05 | 1 | 2 | 1 | 0
2017-07-05 | 1 | 2 | 1 | 0
2017-08-05 | 1 | 0 | 1 | 0
2017-09-05 | 1 | 0 | 1 | 0
2017-10-05 | 1 | 2 | 1 | 1
可以看出,类型列没有求和,而是过去的数据填满了行。例如,第 7 个月的数据使用第 6 个月的相同数据。
以下场景超出了此问题的范围:
如果输入数据框在同一个月内有多行。
Date | ID | Type 1 | Type 2 | Type 3
-----------------------------------------
2017-06-05 | 1 | 2 | 1 | 0
2017-06-19 | 1 | 0 | 1 | 0
2017-10-05 | 1 | 2 | 1 | 1
2017-06-05 | 2 | 0 | 1 | 0
2017-06-25 | 2 | 2 | 0 | 0
2017-09-15 | 3 | 0 | 0 | 5
在这种情况下如何进行聚合,以便每个月每个 ID 仅有一行?
最佳答案
添加天数存在主要问题,因为通过 MS
重新采样 - 月初:
df['Date'] = pd.to_datetime(df['Date'], format='%Y%m%d')
#replace days to 1
t1 = df['Date'].dt.to_period('m').dt.to_timestamp()
a = df['Date'] - t1
#create MultiIndex Series with difference of days from 1's day od month
s = pd.Series(a.values, index=[df['ID'], t1])
print (s)
ID Date
1 2017-06-01 4 days
2017-08-01 4 days
2017-10-01 4 days
2 2017-06-01 4 days
2017-07-01 4 days
3 2017-09-01 14 days
dtype: timedelta64[ns]
#helper df2 for append missing NaNs rows
df2 = df.set_index(['ID','Date'])
#add missing dates with resample by start od month and forward fill NaNs
df1 = df.set_index(['Date']).groupby('ID').resample('MS').ffill()
print (df1)
ID Type 1 Type 2 Type 3
ID Date
1 2017-06-01 NaN NaN NaN NaN
2017-07-01 1.0 2.0 1.0 0.0
2017-08-01 1.0 2.0 1.0 0.0
2017-09-01 1.0 0.0 1.0 0.0
2017-10-01 1.0 0.0 1.0 0.0
2 2017-06-01 NaN NaN NaN NaN
2017-07-01 2.0 0.0 1.0 0.0
3 2017-09-01 NaN NaN NaN NaN
<小时/>
#add missing timedeltas by added rows in df1 by forward filling
s1 = s.reindex(df1.index, method='ffill')
print (s1)
ID Date
1 2017-06-01 4 days
2017-07-01 4 days
2017-08-01 4 days
2017-09-01 4 days
2017-10-01 4 days
2 2017-06-01 4 days
2017-07-01 4 days
3 2017-09-01 14 days
dtype: timedelta64[ns]
#create final MultiIndex with added timedelta by set_index
mux = [df1.index.get_level_values('ID'),
df1.index.get_level_values('Date') + s1.values]
#add missing NaNs rows with combine original
df = df1.drop('ID', 1).set_index(mux).combine_first(df2).reset_index()
print (df)
ID Date Type 1 Type 2 Type 3
0 1 2017-06-05 2.0 1.0 0.0
1 1 2017-07-05 2.0 1.0 0.0
2 1 2017-08-05 2.0 1.0 0.0
3 1 2017-09-05 0.0 1.0 0.0
4 1 2017-10-05 0.0 1.0 0.0
5 2 2017-06-05 0.0 1.0 0.0
6 2 2017-07-05 0.0 1.0 0.0
7 3 2017-09-15 0.0 0.0 5.0
编辑:
#set days to 1
df['Date'] = df['Date'] - pd.offsets.MonthBegin()
#aggregate for unique months
df1 = df.groupby(['Date','ID']).sum()
print (df1)
Type 1 Type 2 Type 3
Date ID
2017-06-01 1 2 2 0
2 2 1 0
2017-09-01 3 0 0 5
2017-10-01 1 2 1 1
#add missing months by resample
df1 = df1.reset_index(['ID']).groupby('ID').resample('MS').ffill()
print (df1)
ID Type 1 Type 2 Type 3
ID Date
1 2017-06-01 1 2 2 0
2017-07-01 1 2 2 0
2017-08-01 1 2 2 0
2017-09-01 1 2 2 0
2017-10-01 1 2 1 1
2 2017-06-01 2 2 1 0
3 2017-09-01 3 0 0 5
关于python - Pandas 按月聚合,以 2 列作为索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47496816/
我有一个 Cassandra 集群,里面有 4 个表和数据。 我想使用聚合函数(sum,max ...)发出请求,但我在这里读到这是不可能的: http://www.datastax.com/docu
我有以下两张表 Table: items ID | TITLE 249 | One 250 | Two 251 | Three 我投票给这些: Table: votes VID | IID | u
这个问题在这里已经有了答案: Update MongoDB field using value of another field (12 个答案) 关闭 3 年前。 我想根据另一个“源”集合的文档中
我的收藏包含以下文件。我想使用聚合来计算里面有多少客户,但我遇到了一些问题。我可以获得总行数,但不能获得总(唯一)客户。 [{ _id: "n001", channel: "Kalip
我有下表 Id Letter 1001 A 1001 H 1001 H 1001 H 1001 B 1001 H 1001 H 1001
得到一列的表 ABC。 “创建”的日期列。所以样本值就像; created 2009-06-18 13:56:00 2009-06-18 12:56:00 2009-06-17 14:02:0
我有一个带有数组字段的集合: {[ name:String buyPrice:Int sellPrice:Int ]} 我试图找到最低和最高买入/卖出价格。在某些条目中,买入或卖出价格为零
我有以下问题: 在我的 mongo db 中,我有以下结构: { "instanceId": "12", "eventId": "0-1b", "activityType":
下面给出的是我要在其上触发聚合查询的 Elasticsearch 文档。 { "id": 1, "attributes": [ { "fieldId": 1,
我正在使用 Django 的 aggregate query expression总计一些值。最终值是一个除法表达式,有时可能以零作为分母。如果是这种情况,我需要一种方法来逃避,以便它只返回 0。 我
我正在学习核心数据,特别是聚合。 当前我想要做的事情:计算表中在某些条件上具有逆关系的多对关系的记录数。 目前我正在这样做: NSExpression *ex = [NSExpression expr
我需要有关 Delphi 中的 ClientDatasets 的一些帮助。 我想要实现的是一个显示客户的网格,其中一列显示每个客户的订单数量。我将 ClientDataset 放在表单上并从 Delp
我的集合有 10M 个文档,并且有一个名为 movieId 的字段;该文档具有以下结构: { "_id" : ObjectId("589bed43e3d78e89bfd9b779"), "us
这个问题已经有答案了: What is the difference between association, aggregation and composition? (21 个回答) 已关闭 9
我在 elasticsearch 中有一些类似于这些示例的文档: { "id": ">", "list": [ "a", "b", "c" ] } { "id"
我正在做一些聚合。但是结果完全不是我所期望的,似乎它们没有聚合索引中与我的查询匹配的所有文档,在这种情况下 - 它有什么好处? 例如,首先我做这个查询: {"index":"datalayer","t
假设我在 ES 中有这些数据。 | KEY | value | |:-----------|------------:| | A |
可能在我的文档中,我有一个被分析的文本字段。我只是在ElasticSearch AggregationAPI中迷路了。我需要2种不同情况的支持: 情况A)结果是带有计数标记(条款)的篮子下降。 情况B
我正在为网上商店构建多面过滤功能,如下所示: Filter on Brand: [ ] LG (10) [ ] Apple (5) [ ] HTC (3) Filter on OS: [ ] Andr
我有一个父/子关系并且正在搜索 child 。 是否可以在父属性上创建聚合? 例如parent 是 POST,children 是 COMMENT。如果父项具有“类别”属性,是否可以搜索 COMMEN
我是一名优秀的程序员,十分优秀!