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

Append new columns to HDFStore with pandas
Pandas: reindex multiindex, broadcast results
TypeError when changing an integer data series in pandas
Pandas file IO Read Error
Pandas — asof() by last on DataFrame
Pandas Column Construction with np.where()
MultiIndex-based indexing in pandas
How to get the number of the most frequent value in a column?
Using pandas.ols on multiple dependent variables at once
Insert 0-values for missing dates within MultiIndex
Reindexing dataframes
pandas access axis by user-defined name
Trouble with groupss and aggregation
Replace MultiIndex's contents with DataFrame columns
What's the `DataFrameGroupBy`-equivalent of `dict.keys`?
How to split a dataframe according to a boolean criterion?

Categories

HOME
osgi
angular-material
relative-path
kalman-filter
jira
infragistics
rubygems
uber-api
append
webrequest
offline
row
dtrace
fortumo
zebra-printers
decimal
iolanguage
ghc
footer
lldb
plunker
tar
sylius
kvc
google-search-api
wijmo
intel-pin
greendao
ejabberd-module
sql-server-2012-express
xquery-3.0
jspresso
subset-sum
fusionpbx
plsql-psp
space-complexity
multilingual
bootstrapper
mesos-chronos
ruby-on-rails-3.1
http-live-streaming
ibpy
http-redirect
import-from-excel
qcombobox
dotnetzip
linode
sqldf
orthogonal
ado.net-entity-data-model
flash-cs5
sigabrt
testng-dataprovider
hexdump
ipconfig
phpcas
unity-networking
kendonumerictextbox
pervasive-sql
reactive-banana
browser-bugs
riak-cs
sankey-diagram
remobjects
sonarqube5.1.2
block-device
typekit
rdtsc
xojo
rebol3
odata4j
ora-00911
back-stack
django-nonrel
comaddin
pvrtc
cisco-jtapi
bluepill
dotnethighcharts
gnu-prolog
nsobject
doh
horizontal-accordion
servicehost
actionview
xfbml
forums
vc90
bespin
substrings
spec#

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