Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - How to check if multi index column exists

Tags:

python

pandas

My question is similar to How to check if a column exists in Pandas but for the multi-index column case.

I'm trying to process values in a multi index column dataframe using column names originating in another file - hence the need to check if the column exists. A representative example is below:

import pandas as pd
from numpy.random import randint,randn

df = pd.DataFrame({ 'A': [randint(0,3) for p in range(0,12)],'B': [0.1* randint(0,3) for p in range(0,12)],
      'C': [0.1*randint(0,3) for p in range(0,12)],'D': randn(12),
    })

df1 = df.groupby(['A','B','C']).D.sum().unstack(-1)
df1 = df1.T
df1
A           0                   1                             2          
B         0.0       0.2       0.0       0.1       0.2       0.0       0.1
C                                                                        
0.0       NaN       NaN       NaN  0.845316       NaN  0.555513       NaN
0.1       NaN  0.139371       NaN       NaN       NaN       NaN -0.260868
0.2  5.002509       NaN  0.637353  0.438863  0.943098       NaN       NaN

df1[1][0.1]
C
0.0    0.845316
0.1         NaN
0.2    0.438863

Accessing df1[0][0.1] in the above example will result in a key error. How do I check if a multi index column exists, so that non-existent columns can be skipped during processing?

Thanks!

like image 740
kip6000 Avatar asked May 22 '16 22:05

kip6000


People also ask

How do I check if multiple columns exist in DataFrame Python?

Check for Multiple Columns Exists in Pandas DataFrame In order to check if a list of multiple selected columns exist in pandas DataFrame, use set. issubset . For Example, if set(['Courses','Duration']). issubset(df.

Can a pandas index contain multiple columns?

MiltiIndex is also referred to as Hierarchical/multi-level index/advanced indexing in pandas enables us to create an index on multiple columns and store data in an arbitrary number of dimensions.


1 Answers

You can think of a multi index like an array of tuples, so can access like:

df1[(0, 0.1)]

and test like:

(0, 0.1) in df1.columns:
like image 124
Mr.F Avatar answered Sep 27 '22 16:09

Mr.F