Make list comprehension of a pandas dataframe

I would like to make a list comprehension of this df. But it didn’t work. Please have a look.

import numpy as np
df = pd.DataFrame({'date': pd.date_range(start = '2020-01-01', end = '2020-12-31', freq = 'H')})
df['dtDate'] = df['date'].dt.date
df['intHour'] = df['date'].dt.time
df['dblPrice'] = 45 + 10*np.random.rand(len(df))
df = df.drop('date', axis = 1)

pivot = df.pivot(index = 'dtDate', columns = 'intHour', values = 'dblPrice')

And got this dataframe

intHour 00:00:00    01:00:00    02:00:00    03:00:00    04:00:00    05:00:00    06:00:00    07:00:00    08:00:00    09:00:00    ... 14:00:00    15:00:00    16:00:00    17:00:00    18:00:00    19:00:00    20:00:00    21:00:00    22:00:00    23:00:00
dtDate                                                                                  
2020-01-01  52.538754   51.266834   51.373766   45.931978   52.609328   50.994931   51.243347   54.008440   53.313223   50.066071   ... 47.631150   49.151051   51.230233   45.994514   46.603801   47.576478   54.277707   47.650023   45.535775   51.245288
2020-01-02  54.591999   54.810271   50.449986   46.800304   49.747234   50.002123   51.382932   53.773221   49.190824   54.865301   ... 48.188451   50.980094   52.985792   53.854782   48.715501   53.419668   45.958799   50.721659   50.025296   46.692690
2020-01-03  50.666929   50.127614   52.988890   49.797910   52.668042   46.745173   53.233271   52.660249   50.293708   54.212875   ... 52.933317   53.053633   49.817718   53.712133   48.702070   53.183621   51.698917   48.601872   45.762772   54.713565
2020-01-04  53.210317   54.504347   48.990980   46.313111   49.630296   54.855403   47.051514   53.416724   52.126807   46.779938   ... 54.823527   48.211544   45.756259   48.990539   54.049532   54.134094   49.649077   49.462418   47.850721   46.913194
2020-01-05  48.525838   51.466914   47.646876   47.655881   51.143606   50.072440   48.010113   51.275604   47.169781   45.386516   ... 49.845797   48.682241   51.143187   52.643786   50.871959   54.621818   47.472802   50.818774   50.479920   54.320502
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2020-12-27  50.223507   50.919980   48.415068   48.236561   45.915901   49.851045   47.235178   45.852695   46.108208   46.500174   ... 53.986690   50.835382   54.937447   47.390783   45.074738   47.800815   53.589714   50.987971   49.597049   52.153157
2020-12-28  50.671287   54.534284   46.404504   47.427804   49.860974   54.858333   53.127082   46.716249   49.664347   52.813928   ... 51.245018   49.970843   54.989815   47.029045   52.105651   48.829855   53.768145   45.260424   48.351198   48.826515
2020-12-29  47.865982   51.279802   45.939696   50.316662   46.864319   50.615450   51.361867   49.743674   49.088331   48.515341   ... 50.221210   47.246730   51.632251   47.740422   47.947939   45.895385   46.152617   52.852860   52.449546   51.313461
2020-12-30  51.101144   50.516915   49.122323   46.505722   53.351985   52.348676   54.501081   46.962222   54.579690   46.476254   ... 50.938915   47.224380   50.406062   54.143802   46.059016   48.459780   48.465229   46.146354   50.951462   48.931579
2020-12-31  49.292937   NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

And trying to do list comprehension of this one:

x = [pivot[t][i] for t in T for i in I]

And getting Keyerror. Can I actually even do that?

Answer

Are you looking for values. Transpose your dataframe and get a flatten array:

np.ravel(pivot.values.T)

Is equivalent to:

x = [pivot[t][i] for t in pivot.columns for i in pivot.index]