Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pick nearest value in dataframe pandas

I have a dataframe and need to pick nearest value if there is Nan.(from open or price_orderbook column) whichever is nearer.

time                open    high    low     close   timestamp    price_orderbook
                        
2022-02-22 19:05:10 2.128   2.129   2.128   2.129   NaN              NaN
2022-02-22 19:05:11 NaN      NaN    NaN       NaN   NaN              NaN
2022-02-22 19:05:12 NaN      NaN    NaN       NaN   NaN              NaN
2022-02-22 19:05:13 NaN      NaN    NaN       NaN   NaN              NaN
2022-02-22 19:05:14 NaN      NaN    NaN       NaN   NaN              NaN
2022-02-22 19:05:15 2.129   2.129   2.129   2.129   NaN              NaN
2022-02-22 19:05:16 2.128   2.128   2.128   2.128   NaN              NaN
2022-02-22 19:05:17 NaN      NaN    NaN       NaN   NaN              NaN
2022-02-22 19:05:18 2.128   2.128   2.128   2.128   1.645557e+09    2.1285
2022-02-22 19:05:19 NaN      NaN    NaN       NaN   NaN              NaN
2022-02-22 19:05:20 NaN      NaN    NaN       NaN   NaN              NaN
2022-02-22 19:05:21 NaN      NaN    NaN       NaN   1.645557e+09    2.1285
2022-02-22 19:05:22 NaN      NaN    NaN       NaN   NaN              NaN
2022-02-22 19:05:23 NaN      NaN    NaN       NaN   NaN              NaN

Example:

df['closer']= "value" if there is open or price_orderbook else search for nearest open or price_orderbook

Is there anything in pandas that helps me out.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can replace mising values from open by non missing values if exist in price_orderbook first, then remove all another missing values by Series.dropna and for nearest values use Series.reindex with method='nearest':

df['closer'] = (df['open'].fillna(df['price_orderbook'])
                          .dropna()
                          .reindex(df.index, method='nearest'))

print (df)
                      open   high    low  close     timestamp  \
2022-02-22 19:05:10  2.128  2.129  2.128  2.129           NaN   
2022-02-22 19:05:11    NaN    NaN    NaN    NaN           NaN   
2022-02-22 19:05:12    NaN    NaN    NaN    NaN           NaN   
2022-02-22 19:05:13    NaN    NaN    NaN    NaN           NaN   
2022-02-22 19:05:14    NaN    NaN    NaN    NaN           NaN   
2022-02-22 19:05:15  2.129  2.129  2.129  2.129           NaN   
2022-02-22 19:05:16  2.128  2.128  2.128  2.128           NaN   
2022-02-22 19:05:17    NaN    NaN    NaN    NaN           NaN   
2022-02-22 19:05:18  2.128  2.128  2.128  2.128  1.645557e+09   
2022-02-22 19:05:19    NaN    NaN    NaN    NaN           NaN   
2022-02-22 19:05:20    NaN    NaN    NaN    NaN           NaN   
2022-02-22 19:05:21    NaN    NaN    NaN    NaN  1.645557e+09   
2022-02-22 19:05:22    NaN    NaN    NaN    NaN           NaN   
2022-02-22 19:05:23    NaN    NaN    NaN    NaN           NaN   

                     price_orderbook  closer  
2022-02-22 19:05:10              NaN  2.1280  
2022-02-22 19:05:11              NaN  2.1280  
2022-02-22 19:05:12              NaN  2.1280  
2022-02-22 19:05:13              NaN  2.1290  
2022-02-22 19:05:14              NaN  2.1290  
2022-02-22 19:05:15              NaN  2.1290  
2022-02-22 19:05:16              NaN  2.1280  
2022-02-22 19:05:17              NaN  2.1280  
2022-02-22 19:05:18           2.1285  2.1280  
2022-02-22 19:05:19              NaN  2.1280  
2022-02-22 19:05:20              NaN  2.1285  
2022-02-22 19:05:21           2.1285  2.1285  
2022-02-22 19:05:22              NaN  2.1285  
2022-02-22 19:05:23              NaN  2.1285  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading