Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>Note: Now that Series have the dt accessor it's less important that date is the index, though Date/Time still needs to be a datetime64.</em></p> <p>Update: You can do the groupby more directly (without the lambda):</p> <pre><code>In [21]: df.groupby([df["Date/Time"].dt.year, df["Date/Time"].dt.hour]).mean() Out[21]: Value Date/Time Date/Time 2010 0 60 1 50 2 52 3 49 In [22]: res = df.groupby([df["Date/Time"].dt.year, df["Date/Time"].dt.hour]).mean() In [23]: res.index.names = ["year", "hour"] In [24]: res Out[24]: Value year hour 2010 0 60 1 50 2 52 3 49 </code></pre> <p>If it's a datetime64 <em>index</em> you can do:</p> <pre><code>In [31]: df1.groupby([df1.index.year, df1.index.hour]).mean() Out[31]: Value 2010 0 60 1 50 2 52 3 49 </code></pre> <hr> <p>Old answer (will be slower):</p> <p>Assuming Date/Time was the index* you can use a mapping function in the <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html" rel="noreferrer">groupby</a>:</p> <pre><code>In [11]: year_hour_means = df1.groupby(lambda x: (x.year, x.hour)).mean() In [12]: year_hour_means Out[12]: Value (2010, 0) 60 (2010, 1) 50 (2010, 2) 52 (2010, 3) 49 </code></pre> <p>For a more useful index, you could then create a MultiIndex from the tuples:</p> <pre><code>In [13]: year_hour_means.index = pd.MultiIndex.from_tuples(year_hour_means.index, names=['year', 'hour']) In [14]: year_hour_means Out[14]: Value year hour 2010 0 60 1 50 2 52 3 49 </code></pre> <p>* if not, then first use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html" rel="noreferrer"><code>set_index</code></a>:</p> <pre><code>df1 = df.set_index('Date/Time') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload