0
arrays = [['indcator_1', 'indcator_1', 'indcator_2', 'indcator_2', 'indcator_3', 'indcator_3', 'indcator_4', 'indcator_4'], ['2018', '2019', '2018', '2019', '2018', '2019', '2018', '2019']]

tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['indicators', 'state'])
df = pd.DataFrame(np.random.randn(3, 8), index=['AZ', 'CA', 'IL'], columns=index)

df
Out[5]: 
indicators indcator_1           indcator_2           indcator_3            \
state            2018      2019       2018      2019       2018      2019   
AZ           0.823890  0.165838  -1.207324  0.991570   0.424288 -0.697368   
CA           0.323375 -1.301540   1.245174 -0.542040  -0.305037 -0.148536   
IL           0.283575  0.095836   0.542553 -0.179618   1.989065  0.914899   

indicators indcator_4            
state            2018      2019  
AZ          -1.544907 -0.286541  
CA           1.168241  1.702455  
IL           0.524246  2.031239  

I want to reshape it like:
this

Is there a way to do that? I could not find a solution for this specific case anywhere.

Sorry for the bad presentation of the question. I'm new here.

Thanks for the help!

dhaval
  • 23
  • 4
  • 1
    Welcome to SO. Please review [ask] and create a [mcve], that means ***no pictures of code***, which was clearly stated in what you were recommended to have read before posting. – user3483203 Jul 29 '19 at 20:51
  • 1
    Those DF's are small enough to paste the text. Images are a bad idea and will get you downvoted. – Paul Dawson Jul 29 '19 at 20:52
  • just write them down by writing the code that can produce them – FabioSpaghetti Jul 29 '19 at 20:52
  • Thank you all for the pointers. I've edited the question accordingly. Will keep in mind the next time :) – dhaval Jul 30 '19 at 06:28

1 Answers1

1

Have you tried stack?

df.stack()
Code Different
  • 90,614
  • 16
  • 144
  • 163