Skip to content
Advertisement

Select colums in pandas multi index dataframe

I probably have a rather simple pandas question, but despite having tried multiple solutions posted on stackoverflow, I can’t figure out how to do it properly.

I have pandas multi-index Dataframe with the following structure:

Image1

now I want to select a subset of this dataframe based on the first column headers (HDx_DATE)

The columns which I want to show are stored in a list, so for example columns_show = ["HD1_DATE", "HD2_DATE"]

After the filtering, the Dateframe should look like this:

Image2

So it should only show the system values and entered Values for the columns in the list.

How can I achieve this?

Thanks a lot for your support

Daniel

Advertisement

Answer

Hello and thank you for the warm welcome. I will take care of theese guidelines in the future.

I finally was able to solve my problem by the help of this post:

multi column selection with pandas xs function is failed

This did the trick for me:

select = df_compare_filtered.columns.get_level_values(0).isin(columns_show)
df_compare_filtered.loc[:, select]

The dataframe looks like this:

       A               B                 C
system | user   system | user     system | user
   1       2       3      8          3      5

lets suppose the list columns show is:

columns_show = ["A", "C"]

Then the resulting dataframe looks like this:

       A               C                  
system | user   system | user
   1       2        3      5
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement