Marcel
Marcel

Reputation: 464

Very strange Pandas behavior while using multi-indexing

I use the following code to create a Pandas DataFrame with multi-index and update some values.

indices = pd.MultiIndex.from_product(iterables=[['X','Y','Z'],['h1','h2','h3']],names=['idx1','idx2'])
df = pd.DataFrame(0.0,index=indices,columns=['A'])

df.loc['X','A']['h1'] = 1.1
df.loc['Y','A']['h1'] = 2.2
df.loc['Z','A']['h1'] = 3.3

print(df)

This code generates the following output, as expected:

             A
idx1 idx2
X    h1    1.1
     h2    0.0
     h3    0.0
Y    h1    2.2
     h2    0.0
     h3    0.0
Z    h1    3.3
     h2    0.0
     h3    0.0

But when I use the following code (note the 'Y' in the first index is moved at the end), the output is wrong:

import pandas as pd
indices = pd.MultiIndex.from_product(iterables=[['X','Z','Y'],['h1','h2','h3']],names=['idx1','idx2'])
df = pd.DataFrame(0.0,index=indices,columns=['A'])

df.loc['X','A']['h1'] = 1.1
df.loc['Y','A']['h1'] = 2.2
df.loc['Z','A']['h1'] = 3.3

print(df)
             A
idx1 idx2
X    h1    0.0
     h2    0.0
     h3    0.0
Z    h1    0.0
     h2    0.0
     h3    0.0
Y    h1    0.0
     h2    0.0
     h3    0.0

What's wrong with my second code? I am doing something really stupid, or is it some sort of expected behavior?

I tried on python 3.7.1 and 3.6.8, with pandas 0.23.4 in both cases.

Upvotes: 1

Views: 81

Answers (1)

jezrael
jezrael

Reputation: 862921

Use tuples for set values in MultiIndex for avoid chained indexing:

df.loc[('X','h1'),'A'] = 1.1
df.loc[('Y','h1'),'A'] = 2.2
df.loc[('Z','h1'),'A'] = 3.3

print(df)
             A
idx1 idx2     
X    h1    1.1
     h2    0.0
     h3    0.0
Z    h1    3.3
     h2    0.0
     h3    0.0
Y    h1    2.2
     h2    0.0
     h3    0.0

Upvotes: 1

Related Questions