Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas DataFrame subselection

The code below shows what I would like to do. Is it possible to do it without the iteritems iteration?

Basically I have a DataFrame df with a MultiIndex, and I have a smaller, boolean DataFrame sdf, without the MultiIndex. I would to use sdf as a preselection/mask to set values in df.

mi = pd.MultiIndex.from_product([['A', 'B'], [1, 2]]) 
df = pd.DataFrame([[0, 1, 2, 3], [1, 2, 3, 4]], columns=mi, index=[0, 1]) 
print(df) 
sdf = pd.DataFrame([[True, False], [False, True]], columns=['A', 'B']) 
print(sdf)
for name, value in sdf.iteritems():
        df.loc[value, name] = 10
print(df)

output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   A     B   
   1  2  1  2
0  0  1  2  3
1  1  2  3  4

       A      B
0   True  False
1  False   True

    A       B    
    1   2   1   2
0  10  10   2   3
1   1   2  10  10

Thank you!

>Solution :

Use DataFrame.mask with DataFrame.reindex:

df = df.mask(sdf.reindex(df.columns, axis=1, level=0), 10)
print(df)
    A       B    
    1   2   1   2
0  10  10   2   3
1   1   2  10  10

Details:

print(sdf.reindex(df.columns, axis=1, level=0))
       A             B       
       1      2      1      2
0   True   True  False  False
1  False  False   True   True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading