Search code examples
pythonpandasdataframekeyobject-slicing

how to get a value by slicing a dataframe by giving three column names?


Hey stack family need some help in getting a value from a pandas dataframe but got stuck up here any help would be appreciated.

I want to get a value from this ohlc dataframe.

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

And here to slice a single value from the efi column first i'm making brick and time_id column as the index and trying this to get the value.

I'm taking the max of the time_id because i need the highest time_id from the dataframe.

ohlc.set_index(['brick_counts' , 'time_id'] , inplace = True)
latest_time = ohlc['time_id'].max()

efi_pos1 = ohlc.loc[(6,int(latest_time)),'efi']

But the issue is it gives me the value in series format having index set in place of giving only a single float number which i need.

Can anyone please prefer me any other slicing method to get the value without applying brick_counts and time_id as index ?

I need the value of efi where brick is equals to 6 and time_id is the highest in the dataframe.


Solution

  • efi_pos1 = ohlc.loc[(ohlc['brick_counts'] == 6) & (ohlc['time_id'] == ohlc['time_id'].max()), 'efi'].values[0]
    
    print(efi_pos1)
    

    Output:

    3200