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 multiindex duplicated only for particular indices

Say I have a Pandas dataframe with multiple indices:

arrays = [["UK", "UK", "US", "FR"], ["Firm1", "Firm1", "Firm2", "Firm1"], ["Andy", "Peter", "Peter", "Andy"]]
idx = pd.MultiIndex.from_arrays(arrays, names = ("Country", "Firm", "Responsible"))
df = pd.DataFrame(np.zeros(4), index = idx)
df

                             0
Country Firm  Responsible     
UK      Firm1 Andy         0.0
              Peter        0.0
US      Firm2 Peter        0.0
FR      Firm1 Andy         0.0

I want to drop duplicated entries of the first two index levels (In the example, rows with "UK" and "Firm1" entries) and keep only the rows, where the third index "Responsible" is equal to "Andy". So I want to drop the second row in this case.

In pandas there is drop_duplicates() but I don’t see how I can i) apply it only on the first two index levels and ii) specify to keep rows with "Andy" and drop the rest (the function only allows for ‘first’ and ‘last’ as arguments)

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

I would be happy for inputs! Many thanks in advance.

>Solution :

Remove if duplicated first 2 levels and no Andy in Responsible level – first use Index.to_frame for DataFrame, test duplicates by DataFrame.duplicated by both first level snad keep=Fales for all dupes and filter only Andy rows by chained another mask by | for bitwise OR:

df1 = df.index.to_frame()

df = df[~df1.duplicated(subset=['Country','Firm'], keep=False) | 
         df1['Responsible'].eq('Andy')]
print (df)
                             0
Country Firm  Responsible     
UK      Firm1 Andy         0.0
US      Firm2 Peter        0.0
FR      Firm1 Andy         0.0
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