Question

I have a multi-index dataframe defined, e.g. as:

import pandas as pd
import numpy as np

dates = pd.date_range('20130101',periods=3,freq='5s')
dates = dates.append(dates)

locations = list('AAABBB')
gascode = ['no2','o3','so2']*2

tup = pd.MultiIndex.from_tuples( zip(locations,gascode,dates), names=['Location','gas','Date'] )

data = pd.DataFrame(data=range(6),index=tup,columns=['val1'])

>>> data

Location gas Date                  val1         
A        no2 2013-01-01 00:00:00     0
         o3  2013-01-01 00:00:05     1
         so2 2013-01-01 00:00:10     2
B        no2 2013-01-01 00:00:00     3
         o3  2013-01-01 00:00:05     4
         so2 2013-01-01 00:00:10     5

Keeping data only from location 'A':

data = data.xs(key='A',level='Location')

Now, I want to create new columns according to the 'gas' index to yield:

Date                   no2   o3   so2
2013-01-01 00:00:00     0    nan  nan
2013-01-01 00:00:05     nan  1    nan
2013-01-01 00:00:10     nan  nan  2

I tried pivoting about the 'date' index to put 'gas' to columns, though this failed.

data = data.pivot(index=data.index.get_level_values(level='date'),
                  columns=situ.index.get_level_values(level='gas'))

I am at a loss of how to achieve this; can anyone recommend an alternative?

Was it helpful?

Solution

You can unstack the result:

In [11]: data.xs(key='A', level='Location').unstack(0)
Out[11]: 
                     val1         
gas                   no2  o3  so2
Date                              
2013-01-01 00:00:00     0 NaN  NaN
2013-01-01 00:00:05   NaN   1  NaN
2013-01-01 00:00:10   NaN NaN    2

[3 rows x 3 columns]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top