0

I have a MultiIndex dataframe where column names have two levels and I'd like to remove a certain column on the second level from each column on the first level (in the example below: remove the age column for each participant, but keep the score column). I don't know how to access it because the names on the first level are different but the name on the second level is always the same ("Age"). I tried different versions of df.drop(columns=[(:, 'Age')]) but of course that didn't work. Any help would be appreciated!

cols = pd.MultiIndex.from_tuples([("F", "Age"), 
                                  ("F", "Score"), 
                                  ("A", "Age"),
                                  ("A", "Score"),
                                  ("X", "Age"),
                                  ("X", "Score")])
data=[[23,300,32,400,45,500], [11,500,54,600,23,400]]

df = pd.DataFrame(data, columns=cols)

0 Answers0