pandas


How to make first level of MultiIndex as the columns?


Say I have a MultiIndex dataframe like:
In [1]: arrays = [['one','one','one','two','two','two'],[1,2,3,1,2,3]]
In [2]: df = pa.DataFrame(randn(6,1),index=pa.MultiIndex.from_tuples(zip(*arrays)),columns=['A'])
In [3]: df
Out[3]:
A
one 1 0.229037
2 -1.640695
3 0.908127
two 1 -0.918750
2 1.170112
3 -2.620850
I would like to change this to a new dataframe, with the columns as the first level index of the MultiIndex dataframe? Is there an easy way? (below an example)
In [12]: dft = df.ix['one']
In [13]: dft = dft.rename(columns={'A':'one'})
In [14]: dft['two'] = df.ix['two']['A']
In [15]: dft
Out[15]:
one two
1 0.229037 -0.918750
2 -1.640695 1.170112
3 0.908127 -2.620850
Perhaps you are looking for pandas.unstack:
In [56]: df
Out[56]:
A
one 1 0.229037
2 -1.640695
3 0.908127
two 1 -0.918750
2 1.170112
3 -2.620850
In [57]: df.unstack(level=0)
Out[57]:
A
one two
1 0.229037 -0.918750
2 -1.640695 1.170112
3 0.908127 -2.620850
Just to add something to this, there is another option of making a multi-index into columns using the reset_index() function. The difference here being that it simply "pops" out the values as new columns. Depends on your usecase:
In [5]: df
Out[5]:
A
one 1 -1.598591
2 -0.354813
3 -0.435924
two 1 1.408328
2 0.448303
3 0.381360
In [6]: df.reset_index()
Out[6]:
level_0 level_1 A
0 one 1 -1.598591
1 one 2 -0.354813
2 one 3 -0.435924
3 two 1 1.408328
4 two 2 0.448303
5 two 3 0.381360

Related Links

Pandas: number of subgroups per index
pandas cut by the mount column
How to get a specific column from a data frame?
Parse salesforce report in pandas dateframe using beatbo
python pandas: how to format big numbers in powers of ten in latex
Division between two numbers in a Dataframe
Remove group rows
Creating a dataframe in pandas with one index column and the second column as a list of different sizes creating boxplot problems
How to expand one row to multiple rows according to its value in Pandas
pandas read_excel() is reading thousands of empty lines and columns
Multiple feature extraction in scikitlearn not working(??)
Convert floats to ints in pandas dataframe
Mypy/typeshed stubs for Pandas
Building MultiGraph from pandas dataframe - “TypeError: unhashable type: 'dict'”
Trying to Impute missing values using scipy but getting error
How to avoid SettingWithCopyWarning in pandas?

Categories

HOME
mockito
kde
rsync
react-redux
electronics
ojdbc
mvvmcross
swagger-ui
medical
nano-server
collectd
netflix
clearcase-ucm
openrefine
angular-ui
apache-metamodel
swiftlint
wkwebview
chromium-embedded
brunch
fluentvalidation
ping
google-cloud-nl
gsoap
ios5
disassembling
web-mining
gesture
libvpx
withings
document.write
graphenedb
fedex
gulp-sourcemaps
texmaker
slick-3.0
hibernate-tools
ionicons
magma
typed-lambda-calculus
android-cursor
capacity
spring-cache
orthogonal
minimization
mediaelement
visual-c++-2008
url-pattern
android-textview
radtreelist
intrusion-detection
transmitfile
simplewebrtc
design-by-contract
marching-cubes
collapse
feedback
tmuxinator
fouc
sysinternals
event-bubbling
graph-api-explorer
skos
bigbluebutton
kineticjs
offloading
typekit
jscript.net
googlemock
starcluster
system.net.webexception
xceed-datagrid
uv-mapping
geos
spring-io
web2py-modules
jplaton
eventual-consistency
plasma
interface-orientation
delphi-6
cisco-jtapi
newtonscript
ohm
gwt-ext
javax.script
recent-documents
appendto
jmock
digest-authentication
xsdobjectgen
preference
castle-monorail
meego
ti-dsp
aquaticprime
port-number
post-build
document-conversion
uiq3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App