Pandas: apply a function to a multiindexed series

I have a series 'incoming' that looks like this:

number.hash                               local_time         
19ace78686acf5772212d77595cb7efdb52788bf  2011-04-29 12:00:00    1
1a84708ae329e17438e8157165f91f3dec468eb6  2011-04-25 17:00:00    1
1f5b196086ca35e752eb39e4e348ae925d030af9  2011-02-16 14:00:00    1
                                          2011-02-16 15:00:00    0
                                          2011-02-16 16:00:00    0

, where numbers.hash and local_time together is a MultiIndex. Now I want to apply any function to each series indexed by numbers.hash only, e.g. summing the values in each time series that is made up of local_time and the value. I guess I can get the number.hash indices and iterate over them, but there must be a more efficient and clean way to do it.

Answers


In [36]: s = Series([1,1,1,0,0],pd.MultiIndex.from_tuples([
('A',Timestamp('20110429 12:00:00')),
('B',Timestamp('20110425 17:00:00')),
('C',Timestamp('20110216 14:00:00')),
('C',Timestamp('20110426 15:00:00')),
('C',Timestamp('20110426 16:00:00'))]))


A  2011-04-29 12:00:00    1
B  2011-04-25 17:00:00    1
C  2011-02-16 14:00:00    1
   2011-04-26 15:00:00    0
   2011-04-26 16:00:00    0
dtype: int64

Sum by the level (these are vectorized and very fast)

In [37]: s.sum(level=0)
Out[37]: 
A    1
B    1
C    1
dtype: int64

Or groupby and apply an arbitrary function

In [38]: s.groupby(level=0).apply(lambda x: x.sum())
Out[38]: 
A    1
B    1
C    1
dtype: int64

Need Your Help

Java, Hibernate, CascadeTypes and 'garbage collecting' Orphans

java hibernate orm entity-relationship garbage

What kind of cascade type can I use, and where, to have Hibernate automatically remove the Image when there are no more "Things" referring to it? (so sort of Garbagecollecting in Hibernate, basical...

Surveymonkey API: check if a specific email has completed survey

python surveymonkey

I have a large amount of users (over 400k) that have been sent a survey to complete. As part of logging into my site I'm using the surveymonkey api to check to see if they completed their assigned

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.