user1953031
user1953031

Reputation: 41

Lookup value in dataframe using multiindex

I am looking up a value in a dataframe using a multi-index. df[value1,value2]. This works, but throws a keyerror if the value is not in the index. I can handle the exception but is there an equivalent syntax to a python dict.get()? That is, I would prefer the lookup to return None if the value is not found.

Mark

Upvotes: 2

Views: 4159

Answers (2)

silencer
silencer

Reputation: 2425

If your df has a multiindex in columns 'key1' and 'key2' and you want to look up value xxx on key1 and yyy on key2 , try this

df.ix[df.index.get_level_values('key1') == xxx & 
df.index.get_level_values('key2') == yyy]

Upvotes: 0

Phillip Cloud
Phillip Cloud

Reputation: 25662

Just call DataFrame.get():

In [50]: from pandas.util.testing import makeCustomDataframe as mkdf

In [51]: df = mkdf(5, 2, c_idx_nlevels=2, data_gen_f=lambda *args: rand())

In [52]: df
Out[52]:
C0       C_l0_g0  C_l0_g1
C1       C_l1_g0  C_l1_g1
R0
R_l0_g0    0.155    0.989
R_l0_g1    0.427    0.330
R_l0_g2    0.951    0.720
R_l0_g3    0.745    0.485
R_l0_g4    0.674    0.841

In [53]: level = df.columns[0]

In [54]: level
Out[54]: ('C_l0_g0', 'C_l1_g0')

In [55]: df.get(level)
Out[55]:
R0
R_l0_g0    0.155
R_l0_g1    0.427
R_l0_g2    0.951
R_l0_g3    0.745
R_l0_g4    0.674
Name: (C_l0_g0, C_l1_g0), dtype: float64

In [56]: df.get('how are you?')

In [57]: df.get('how are you?', 'Fine')
Out[57]: 'Fine'

You can also just define a function:

def get_from_index(df, key, default=None):
    try:
        return df.loc[key]
    except KeyError:
        return default

Upvotes: 0

Related Questions