Home > Back-end >  how to get a value by slicing a dataframe by giving three column names which also includes NaN value
how to get a value by slicing a dataframe by giving three column names which also includes NaN value

Time:01-18

This is the ohlc dataframe from which i want to slice the value of efi.

brick_counts    time_id    efi
   1              nan      1000
   2              nan      1500
   3              nan      2000
   4               2       2500 
   5               2       2600
   6               2       3200 

To slice a single value from this ohlc dataframe i'm using this line of code ,

ohlc.loc[(ohlc['brick_counts'] == -5) & (ohlc['time_id'] == 2 ), 'efi'].values[0]

This line works perfectly and gives me value but when i try to apply this line for slicing a value using nan ,

ohlc.loc[(ohlc['brick_counts'] == 5) & (ohlc['time_id'] == float("nan")), 'efi'].values[0]

It shows me this error

IndexError: index 0 is out of bounds for axis 0 with size 0

so how shall i slice the value of efi using brick_counts and nan value of time_id column as my input ?

CodePudding user response:

If always exist at least value use Series.isna:

ohlc.loc[(ohlc['brick_counts'] == 5) & (ohlc['time_id'].isna()), 'efi'].values[0]

If possible no match use next with iter:

out = next(iter(ohlc.loc[(ohlc['brick_counts'] == 5000) & (ohlc['time_id'].isna()), 'efi']), 'no match')
  •  Tags:  
  • Related