我有一个如下所示的哈希列表。
import pandas as pd
import datetime
rows = [{
"version" : "v1",
"timestamp" : "2013-06-04T06:00:00.000Z",
"event" : {
"campaign_id" : "cid2504649263",
"country" : "AU",
"region" : "Cairns",
"impressions" : 3000
}
},
{
"version" : "v1",
"timestamp" : "2013-06-04T06:00:00.000Z",
"event" : {
"campaign_id" : "cid2504649263",
"country" : "AU",
"region" : "Cairns",
"impressions" : 3000
}
},
{
"version" : "v1",
"timestamp" : "2013-06-04T07:00:00.000Z",
"event" : {
"campaign_id" : "cid2504649263",
"country" : "AU",
"region" : "Cairns",
"impressions" : 3000
}
}
]
hash_data = []
for row in rows:
ts = row['timestamp']
meta = row['event']
ts = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S.000Z')
meta['utcdt']=ts
hash_data.append(meta)
data = pd.DataFrame(hash_data)
print data.values
grouped = data.groupby(['utcdt','campaign_id','region','country']).sum()
print grouped.values
[['cid2504649263' 'AU' 3000 'Cairns' datetime.datetime(2013, 6, 4, 6, 0)]
['cid2504649263' 'AU' 3000 'Cairns' datetime.datetime(2013, 6, 4, 6, 0)]
['cid2504649263' 'AU' 3000 'Cairns' datetime.datetime(2013, 6, 4, 7, 0)]]
我的问题是这个。我需要按时间汇总数据。数据应如下所示。我如何在熊猫中做到这一点?
[
['cid2504649263' 'AU' 6000 'Cairns' datetime.datetime(2013, 6, 4, 6, 0)]
['cid2504649263' 'AU' 3000 'Cairns' datetime.datetime(2013, 6, 4, 7, 0)]]
如果使用以下:
grouped = data.groupby(['utcdt','campaign_id','region','country']).sum()
print grouped.values
[[ 6000.]
[ 3000.]]