2
$\begingroup$

My objective is to build an N-dimensional array in Python that has the mean of the intersection of a number of features.

For example, I have the number of customers in a store at any given time. I want to create a three dimensional array with axes [month, day_of_week, hour] where the values are the mean of the historical values.

One example data point, where the data covers several years but each axis here is just 12 months, 7 days, and 24 hours.

Array[January,Friday,1pm] = 3 customers on average

I've tried Pandas Groupby but I can't figure out how to translate that information from a Groupby object or Panel to a useful 3D array.

My closest attempt so far:

dataframe.groupby(["month","day_of_week","hour"])["count"].mean().unstack().to_panel()

This outputs a Panel object, but I can't find a way to translate this to just a 3D array.

Can anyone help me figure out a way to solve my problem and build this 3D array without for loops?

$\endgroup$
3
  • $\begingroup$ What is the exact format you want the data to be in? Is it like a dictionary - {(January,Friday,1pm): 3} for 3d plotting? $\endgroup$ Apr 15 '18 at 4:55
  • $\begingroup$ @KiriteeGak A numpy array, for 3D plotting and cluster analysis. The features wouldn't be strings, just the numerical indices. $\endgroup$ Apr 15 '18 at 6:57
  • $\begingroup$ numpy is v weak for grouping. But using pandas to do the job instead is a mess. I'm also looking for a workable solution. $\endgroup$ Mar 10 '20 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.