Given two dataframes:
df_1:
c1, c2
a, 0
b, 0
c, 2
df_2:
c3, c4
d, 2
e, 3
f, 7
How would you find the closest c2 row to the closest c4 row, and create a new dataframe which has these in corresponding rows next to each other?
Example output:
a, d (zero is closest to two)
b, d (second entry of zero counts as being closest to two)
c, d (this is the closest)
You could start by creating a new dataFrame which is a copy of your original dataframe containing c1, c2
.
Then by using an apply function on the c2
column, you can use the following command to find the closest value and assign it to another column.
result_index = df['col_to_search'].sub(search_value).abs().idxmin()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.