Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to unstack multiindex pandas

import pandas as pd
import numpy as np
arrays = [['Monday','Monday','Tursday','Tursday'],
                        ['Morning','Noon','Morning','Evening']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['Weekday', 'Time'])
df = pd.DataFrame(np.random.randint(5, size=(4,2)), index=index)

In [39]: df
Out[39]: 
                 0  1
Weekday Time         
Monday  Morning  1  3
        Noon     2  1
Tursday Morning  3  3
        Evening  1  2

In [40]: pd.DataFrame(df.to_records())
Out[40]: 
   Weekday     Time  0  1
0   Monday  Morning  1  3
1   Monday     Noon  2  1
2  Tursday  Morning  3  3
3  Tursday  Evening  1  2
Comment

PREVIOUS NEXT
Code Example
Python :: pong code python 
Python :: Merge multiple dataframs 
Python :: TypeError: can only concatenate str (not "method") to str 
Python :: python anonymous object 
Python :: python 3.6 release date 
Python :: how to access dictionary inside an array python 
Python :: python left string 
Python :: how to add to end of linked list python 
Python :: pandas extracting tables from pdf 
Python :: how to find the shortest word in a list python 
Python :: pandas mask multiple condition 
Python :: printing in python 
Python :: pyglet on button press 
Python :: google codelabs 
Python :: wavelet transform in machine learning 
Python :: nested input python 
Python :: plant python documentation 
Python :: elavon converge api python tutorial 
Python :: python char to hex 
Python :: line continutation in r string python 
Shell :: chrome inspect devices 
Shell :: git store credential 
Shell :: list npm packages installed globally 
Shell :: delete all zone identifier files 
Shell :: mvn clean install skip test 
Shell :: logstash is not listening on ip address 
Shell :: install java 17 ubuntu 
Shell :: kill the port in mac 
Shell :: update packages ubuntu 
Shell :: composer install production 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =