Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

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.

1 Answer

0 votes
by (25.1k points)

You can use tuples for set values in MultiIndex:

df.loc[('X','h1'),'A'] = 1.1

df.loc[('Y','h1'),'A'] = 2.2

df.loc[('Z','h1'),'A'] = 3.3

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...