Iterating through dictionary keys when the keys point to an array?

Viewed 58

I made a dictionary where each key references an array. And I'm trying to apply a .loc command to each array, but I can't seem to figure out how to do this. Was wondering how this can be done:

#setup:
import pandas as pd
psd = pd.read_csv('psd.csv')
psd.head()

Out[43]: 
        id group   band       fp1  ...        fz        cz        pz        oz
0  3407906   Cu-  delta  0.291945  ...  0.230908  0.221589  0.247830  0.229259
1  3407906   Cu-  theta  0.022924  ...  0.026598  0.036143  0.029904  0.025336
2  3407906   Cu-  alpha  0.003124  ...  0.017416  0.015175  0.015207  0.028024
3  3407906   Cu-   beta  0.000573  ...  0.005325  0.006429  0.003628  0.003757
4  3407906   Cu-  gamma  0.000485  ...  0.002627  0.002037  0.000956  0.001303

gp = dict(tuple(psd.groupby('band')))
list(gp.keys())

Out[44]: ['alpha', 'beta', 'delta', 'gamma', 'theta']

for key in gp: 
    print(gp[key].head(1))

Out[45]:
        id group   band       fp1  ...        fz        cz        pz        oz
2  3407906   Cu-  alpha  0.003124  ...  0.017416  0.015175  0.015207  0.028024

[1 rows x 32 columns]
        id group  band       fp1  ...        fz        cz        pz        oz
3  3407906   Cu-  beta  0.000573  ...  0.005325  0.006429  0.003628  0.003757

[1 rows x 32 columns]
        id group   band       fp1  ...        fz        cz       pz        oz
0  3407906   Cu-  delta  0.291945  ...  0.230908  0.221589  0.24783  0.229259

[1 rows x 32 columns]
        id group   band       fp1  ...        fz        cz        pz        oz
4  3407906   Cu-  gamma  0.000485  ...  0.002627  0.002037  0.000956  0.001303

[1 rows x 32 columns]
        id group   band       fp1  ...        fz        cz        pz        oz
1  3407906   Cu-  theta  0.022924  ...  0.026598  0.036143  0.029904  0.025336

So far so good, but when I try the following:

for key in gp:
    band = gp[key]
    x_band = band.loc[:,'fp1':'oz']

x_band.head()
Out[49]: 
         fp1        f3        f7  ...        cz        pz        oz
1   0.022924  0.019966  0.021740  ...  0.036143  0.029904  0.025336
6   0.028581  0.026947  0.025785  ...  0.028249  0.028188  0.025704
11  0.005078  0.001958  0.003227  ...  0.002071  0.002150  0.002991
16  0.014026  0.030532  0.018575  ...  0.036702  0.027133  0.020489
21  0.002640  0.001335  0.002605  ...  0.001256  0.001974  0.003032

The result as you can see is only for the last key/array, rather than a dictionary of 5 arrays with the band.loc command applied to it. I could/did just do this manually and ended up with 5 different arrays. But is there a way to keep it in dictionary format so I don't have to do everything from this step out 5 times? Happy to clarify, sorry this is weirdly worded.

0 Answers
Related