简体   繁体   中英

Python pandas multiindex getting info from series

I created a multiindex pandas series from a timeseries and now I want to read the data in it. In all the examples I've seen the columns or levels of the series are named. However, this is not the case in my series. In this multiindex, the first level is the date, and the second level is the hour of the day. The data column has the value I want to read.

What is the easiest way of getting the data I want from my series? The code below should be pretty self explanatory.

   import pandas as pd
   import numpy as np

   n = 1000
   t = pd.date_range(start ='2012-01-01', periods=n, freq='10T')
   x = np.random.randn(n)
   df = pd.Series(data=x, index=t)


   df1 = df[(df > 1) & (df < 1.5)]
   df2 = df1.groupby([df1.index.date, df1.index.hour]).count()

   df2.head(15)
   #How do I get the data out of df2?
   #For example, I want to read the data for '2012-01-02 01:00'

You can access the elements in the multi-index series by providing both labels in a tuple. Eg:

In [19]: df2[(datetime.date(2012,1,2), 3)]
Out[19]: 2

However, this is not so convenient. So I think it is better in this case NOT to construct a multi-index.
You could convert the existing multi-index to a flat one, but a better approach is here I think to do the groupby slightly different. Using the Grouper object I can specify to group the DatetimeIndex on each hour:

In [120]: df2 = df1.groupby(pd.Grouper(freq='H')).count()

In [121]: df2.head()
Out[121]:
2012-01-01 02:00:00    2
2012-01-01 03:00:00    1
2012-01-01 04:00:00    2
2012-01-01 05:00:00    1
2012-01-01 06:00:00    1
Freq: H, dtype: int64

In [123]: df2['2012-01-02 03:00']
Out[123]: 2

As you see, the result has still a DatetimeIndex but with an hourly frequency. Because of this, you can easily index with a datetime string (which was not possible with the multi-index approach)

Note: the groupby from above is actually equivalent to the simpler resample :

df1.resample('H', how='count')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM