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

Is it possible to do this pivot in pandas?
How to replace different strings with different values pretty in a column of dataframe by pandas?
Is there a Pandas DataFrame implementation that loads lazily records from a table in a HDF5 file?
Creating new columns with pd.get_dummies based off boolean filtering
Nan results when concatenating dataframes of different indices
Adding a conditional column to a panda's multiindex dataframe
Equivalent of Try except for Pandas Eval
Pandas how to use boolean indexing to update column from second dataframe column?
Best format for Pandas serialization on disk
How to display the suffix of long strings in Pandas
pivot_table on multi-indexed dataframe
Two unaligned Pandas Series: concat raises error, adding does not but it returns a weird answer
SKLearn MinMaxScaler - scale specific columns only
Plotting separate plots for each decile in time series data
how to convert pandas dataframe to libsvm format?
Merging two dataframes based on a date between two other dates without a common column

Categories

HOME
hook
keycloak
keras
openstack
mediawiki
mockito
relative-path
fft
raspberry-pi
syntax
grep
spring-cloud-stream
cmd
amazon-ecs
frameworks
twitter-bootstrap-4
serverspec
flyway4
pheatmap
communication
nano-server
openedx
emulator
nas
kryo
spark-jobserver
intel-pin
kannel
tooltipster
social-media
spring-mybatis
skia
contact-form
google-sites-2016
code-contracts
calibre
catalog
wpf-controls
squib
graphenedb
broadcastreceiver
sfdc
glew
node-gyp
android-mediaprojection
password-encryption
acoustics
sencha-touch-2.3
thin
scorm
long-polling
scrollable
youcompleteme
jquery-nestable
typescript1.8
togetherjs
statsd
knockout-components
python-cryptography
epson
linode
hittest
parallel-data-warehouse
jquery-filter
firmata
url-pattern
vmware-tools
home-directory
nsviewcontroller
superstack
master-slave
uid
qtableview
t4mvc
qdialog
tmuxinator
citrus-pay
kcachegrind
nsight
system32
bitcoinj
typeof
typo3-neos
intentservice
prettify
gnu-smalltalk
commoncrypto
seed
sabredav
clipper
cisco-jtapi
coderush
infobox
gdlib
html-input
digest-authentication
mirah
multi-tier
memory-size
swing-app-framework
w3wp

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App