【发布时间】:2016-08-14 20:50:35
【问题描述】:
我正在查询数据库并填充熊猫数据框。我正在努力聚合数据(通过 groupby),然后操作数据框索引,使表中的日期成为索引。 这是一个示例,说明了 groupby 前后数据的外观以及我最终要查找的内容。
数据框 - 填充数据
firm | dates | received | Sent
-----------------------------------------
A 10/08/2016 2 8
A 12/08/2016 4 2
B 10/08/2016 1 0
B 11/08/2016 3 5
A 13/08/2016 5 1
C 14/08/2016 7 3
B 14/08/2016 2 5
首先我想按“公司”、“日期”和“接收/发送”分组。
然后操作 DataFrame,使日期成为索引 - 而不是行索引。
最后为每一天添加一个总列
有些公司在某些日子里没有“活动”,或者至少在接收或发送方面没有活动。但是,由于我想查看过去 X 天的情况,因此不可能使用空值,而是需要填写零作为值。
dates | 10/08/2016 | 11/08/2016| 12/08/2016| 13/08/2016| 14/08/2016 firm | ---------------------------------------------------------------------- A received 2 0 4 5 0 sent 8 0 2 1 0 B received 1 3 1 0 2 sent 0 5 0 0 5 C received 0 0 2 0 1 sent 0 0 1 2 0 Totals r. 3 3 7 5 3 Totals s. 8 0 3 3 5
我试过以下代码:
df = > mysql query result
n_received = df.groupby(["firm", "dates"
]).received.size()
n_sent = df.groupby(["firm", "dates"
]).sent.size()
tables = pd.DataFrame({ 'received': n_received, 'sent': n_sent,
},
columns=['received','sent'])
this = pd.melt(tables,
id_vars=['dates',
'firm',
'received', 'sent']
this = this.set_index(['dates',
'firm',
'received', 'sent'
'var'
])
this = this.unstack('dates').fillna(0)
this.columns = this.columns.droplevel()
this.columns.name = ''
this = this.transpose()
基本上,根据这段代码,我没有得到我想要的结果。 - 我怎样才能做到这一点? - 从概念上讲,有没有更好的方法来实现这个结果?比如说在 SQL 语句中进行聚合,或者从优化的角度和逻辑上来说,Pandas 中的聚合是否更有意义。
【问题讨论】:
标签: python datetime pandas data-manipulation