Reputation: 306
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
Reputation: 77860
Most of all, this will be two independent computations. For a given value x
in column 0 ...
diff0
= abs(x - df["0"])min0
) the value min(diff0)min0
] into df2Can you do the specific coding, now that you have a (relatively) simple algorithm?
Upvotes: 1
Reputation: 323356
Try with sub
and idxmin
s=df.sub(df1.values).abs().idxmin()
Out[38]:
0 2
1 1
dtype: int64
Upvotes: 2