Awitz
Awitz

Reputation: 306

pandas find the nearest variables index from multiple columns

I have a sorted DataFrame that looks like this,

df = 
       0      1
0   -0.3   -0.2
1   -0.1   -0.1
2    0.4    0.2
3    0.7    0.5
4    2.0    0.8

and another DataFrame that looks like this,

df1 = 
       0    1
0    0.5 -0.1

I would like the variables in df1 to find their nearest respective value in df and write their index. The expected outlook would be something like this

df2 = 
       0    1
0    0.5 -0.1
1      2    1

Would some type of comprehensions work? Having a lot of trouble with this.

Upvotes: 1

Views: 346

Answers (2)

Prune
Prune

Reputation: 77860

Most of all, this will be two independent computations. For a given value x in column 0 ...

  • generate the vector diff0 = abs(x - df["0"])
  • locate (by index, min0) the value min(diff0)
  • then insert df["0"] [row min0] into df2

Can you do the specific coding, now that you have a (relatively) simple algorithm?

Upvotes: 1

BENY
BENY

Reputation: 323356

Try with sub and idxmin

s=df.sub(df1.values).abs().idxmin()
Out[38]: 
0    2
1    1
dtype: int64

Upvotes: 2

Related Questions