Daniel Marchand
Daniel Marchand

Reputation: 644

Generating Multiple series from multiple indexes

I'd like to generate multiple series from the following dataframe, ideally also plot them on the same figure.

import numpy as np 
import pandas as pd 

# setting up data 
ind1 = np.mod(np.arange(0, 3*4), 3)
ind2 = np.mod(np.repeat(np.array([[1,2,3]]), 4), 3)
y = 2*ind1 - 0.5*ind2 
df = pd.DataFrame({'ind1':ind1,'ind2':ind2,'y':y})
df.set_index(['ind1','ind2'], inplace=True)

Where the first series, y_1 would be something like y_1 = df.ind1==0, y_2 = df.ind2==1 and so on.

I can imagine long and clunky ways of doing this operation 'manually' but I feel certain there is an elegant one or two liner way of handling this that I'm missing...

Upvotes: 2

Views: 54

Answers (1)

pansen
pansen

Reputation: 6663

Here is a quick solution even though I'm not sure if it fits what you're looking for:

# get number of index levels
lvl_cnt = len(df.index.levels)

# iterate each level, and add column to dataframe
for idx in range(lvl_cnt):
    label = "y_{}".format(idx+1)
    values = df.index.get_level_values(idx)
    df[label] = (values == idx).astype(int)

print(df)

                 y     y_1  y_2
ind1    ind2            
0       1      -0.5    1    1
1       1       1.5    0    1
2       1       3.5    0    1
0       1      -0.5    1    1
1       2       1.0    0    0
2       2       3.0    0    0
0       2      -1.0    1    0
1       2       1.0    0    0
2       0       4.0    0    0
0       0       0.0    1    0
1       0       2.0    0    0
2       0       4.0    0    0

Upvotes: 1

Related Questions