2

I am currently working on a problem in which I need to work with binned data. My goal is to get the values of Alpha t1 and Alpha t2 which corresponds to a given Beta value. In this dataframe bellow you can visualize what I mean. Let's say that I have a Beta value of 22.3, then I want to access the Alpha t1 and Alpha t2 out of row 5.

         Beta Bins  Alpha t1  Alpha t2
1       (0.0, 5.0]       0.0       0.0
2      (5.0, 10.0]       0.0       0.0
3     (10.0, 15.0]       0.0       0.0
4     (15.0, 20.0]       0.0       0.0
5     (20.0, 25.0]       0.0       0.0
6     (25.0, 30.0]       0.0       0.0
7     (30.0, 35.0]       0.0       0.0
8     (35.0, 40.0]       0.0       0.0
9     (40.0, 45.0]       0.0       0.0
10    (45.0, 50.0]       0.0       0.0
11    (50.0, 55.0]       0.0       0.0
12    (55.0, 60.0]       0.0       0.0
13    (60.0, 65.0]       0.0       0.0
14    (65.0, 70.0]       0.0       0.0

So, as I am trying to access this value in a for loop, I have been trying the following:

for i, val in enumerate(Beta):

      dfi = df.loc[(df['Beta Bins'] == val)]

However, I am only receiving back an empty dataframe back. I would like to localize the row which corresponds to the interval in which my value is set in. So, is it possible to access that binned data by referencing an specific value?

EDIT

This is how I generate the data:

import numpy as np
import pandas as pd

bins = np.linspace(0,360,73)
alpha0_1 = np.tile(0, len(bins))
alpha0_2 = np.tile(0, len(bins))        
df = pd.DataFrame([bins,alpha0_1,alpha0_2]).T
df.columns = ['Beta Bins', 'Alpha 1', 'Alpha 2']
df['Beta Bins'] = pd.cut(df['Beta Bins'], bins)
df = df.dropna()

Hope that I managed to be clear.

fnaos
  • 151
  • 1
  • 12

1 Answers1

4

UPDATED

df.set_index('Beta Bins').loc[[val]].reset_index()

Examples

df.set_index('Beta Bins').loc[[2.5]].reset_index()
#    Beta Bins  Alpha 1  Alpha 2
#0  (0.0, 5.0]      0.0      0.0

df.set_index('Beta Bins').loc[[2.5,3,5,6]].reset_index()
#     Beta Bins  Alpha 1  Alpha 2
#0   (0.0, 5.0]      0.0      0.0
#1   (0.0, 5.0]      0.0      0.0
#2   (0.0, 5.0]      0.0      0.0
#3  (5.0, 10.0]      0.0      0.0

- Creating a list of DataFrame

list_select = [1,9,10,2,3,300,200,32,45]
df_l = [df.set_index('Beta Bins').loc[[val]].reset_index() for val in list_select]

- Creating a dict of DataFrame

df_d = {i:df.set_index('Beta Bins').loc[[val]].reset_index() for i,val in enumerate(list_select,1)}

See: Indexing with an IntervalIndex

ansev
  • 30,322
  • 5
  • 17
  • 31
  • Interesting suggestion. However, I tried both here and got only errors. For the first one: ValueError: can only insert Interval objects and NA into an IntervalIndex. For the second one: TypeError: cannot do label indexing on with these indexers [233.5] of . I will edit my question and add my dataset. – fnaos Jan 23 '20 at 14:37
  • I have verified that the second method is not valid on the other hand, so that you can use reset_index () it is necessary that `.loc` return a data frame so you always have to pass it a list. Check it now :) – ansev Jan 23 '20 at 15:07