Indexing a pandas DataFrame

I have Multindex DataFramewith the following structure:

       0     1     2     ref
A  B             
21 45  0.01  0.56  0.23  0.02
22 45  0.30  0.88  0.53  0.87
23 46  0.45  0.23  0.90  0.23

What I want to do with this:
From the [0: 2] columns, select the closest value in the "ref" column, so the expected result would be:

       closest
A  B             
21 45  0.01
22 45  0.88
23 46  0.23 
+5
source share
1 answer

Recovery DataFrame:

In [1]: index = MultiIndex.from_tuples(zip([21,22,23],[45,45,46]), names=['A', 'B'])
In [2]: df = DataFrame({0:[0.01, 0.30, 0.45], 
                        1:[0.56, 0.88, 0.23], 
                        2:[0.23, 0.53, 0.90], 
                        'ref': [0.02, 0.87, 0.23]}, index=index)
In [3]: df
Out[3]: 
        0     1     2   ref
A  B                         
21 45  0.01  0.56  0.23  0.02
22 45  0.30  0.88  0.53  0.87
23 46  0.45  0.23  0.90  0.23

First, I would get the absolute distance of the columns 0, 1and 2from ref:

 In [4]: dist = df[[0,1,2]].sub(df['ref'], axis=0).apply(np.abs)
 In [5]: dist
 Out[5]: 
         0     1     2
 A  B                   
 21 45  0.01  0.54  0.21
 22 45  0.57  0.01  0.34
 23 46  0.22  0.00  0.67

Given now dist, you can define a column with a min value per row using DataFrame.idxmin:

In [5]: idx = dist.idxmin(axis=1)
In [5]: idx
Out[5]: 
A   B 
21  45    0
22  45    1
23  46    1

To create a new one closest, you just need to use idxfor indexing df:

In [6]: df['closest'] = idx.index.map(lambda x: df.ix[x][idx.ix[x]])
In [7]: df
Out[7]: 
        0     1     2   ref  closest
A  B                                  
21 45  0.01  0.56  0.23  0.02     0.01
22 45  0.30  0.88  0.53  0.87     0.88
23 46  0.45  0.23  0.90  0.23     0.23

, Pandas , .

+4

All Articles