the resuts of my groupby.groups return :
{(2014, 36): [2], (2014, 41): [3, 4], (2013, 10): [0], (2014, 48): [5], (2014, 37): [1]}
i want to convert it into a dataframe that will looks like :
2013 2014
10 1 0
36 0 1
37 0 1
41 0 2
48 0 1
-- adds -- here is my workflow to the groupby.groups results :
def tr_epoch(epoch):
y,wn,dn = epoch.isocalendar()
return y, wn
d = [1362826800, 1410260400, 1409828400, 1412766000, 1412769600, 1417262400 ]
l = map(lambda x: tr_epoch(datetime.datetime.fromtimestamp(x)), d)
df = pd.DataFrame(l, columns=['year','week_idx'])
res = df.groupby(['year','week_idx']).groups
-- adds -- in pythonic way, using iteration, i will do :
def to_dict(k,v):
yr, wk = k
return {'week': wk, yr: len(v)}
data = map(lambda(k,v): to_dict(k,v), res.iteritems())
df = pd.DataFrame.from_records(data, index='week').fillna(0).sort()
But i am sure, there is a pandas way to do.
So you want to calculate the size of each group? Then you can do:
In [31]: df.groupby(['year','week_idx']).size()
Out[31]:
year week_idx
2013 10 1
2014 36 1
37 1
41 2
48 1
dtype: int64
To reshape this to the expected output, we can now use unstack
to move the 'year' index level from the rows to the columns (and the use fillna
to get 0's):
In [33]: df.groupby(['year','week_idx']).size().unstack(0).fillna(0)
Out[33]:
year 2013 2014
week_idx
10 1 0
36 0 1
37 0 1
41 0 2
48 0 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.