pythonpandasdataframemulti-index

add rows of zeros to multiindex dataframe


I have a multiindex dataframe called 'prevtests', which for testing purposes I have added one entry to:

                            tests  fails
thickness sample size pval              
4         10          0.05      0      0

I have a multiindex, with the same level names, called multindex

MultiIndex([(3.5, 2, 0.05),
            (4.0, 2, 0.05),
            (3.5, 5, 0.05),
            (4.0, 5, 0.05)],
           names=['thickness', 'sample size', 'p value'])

and I want to add zero rows to prevtests for each entry in multindex so that prevtests looks like this:

                            tests  fails
thickness sample size pval              
3.5       2           0.05      0      0
4         2           0.05      0      0
3.5       5           0.05      0      0
4         5           0.05      0      0
4         10          0.05      0      0

I don't care what order the rows are in. If entries in multindex are already in prevtests I want those entries to be left alone. I used the code

prevtests.reindex(prevtests.index.union(multindex), fill_value=0)

but this didn't make any change to prevtests. I don't understand why- I thought this was precisely what I was looking for.


Solution

  • The reindex function returns a new DataFrame with the reindexed values but won't modify prevtests in place unless you assign the result back to prevtests.like this :-

    prevtests = prevtests.reindex(prevtests.index.union(multindex), fill_value=0)