0

I have a data frame with a column containing arrays (all 1x9 arrays). For all rows in that column, I wish to find the ones where the third element is 1 and pick out the values from another column in the corresponding row. For example, I wish to pick out the 'cal_nCa' value (116) where the second element in info_trig is 0

    info_trig                        cal_nCa
0   [0, 1, 0, 0, 0, 0, 0, 0, 0]        128   
1   [0, 1, 0, 0, 0, 0, 0, 0, 0]         79  
2   [0, 0, 0, 1, 0, 0, 0, 1, 0]        116   
3   [0, 1, 0, 0, 0, 0, 0, 0, 0]         82

I tried something in line of df["A"][(df["B"] > 50)], based on Selecting with complex criteria from pandas.DataFrame.

When selecting the desired rows:

data["info_trig"][:][3]

I only succeed selecting a specific row and the third element in that row. But unable to select all the third element in every row. A loop could work but I hope there is a cleaner way out.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114

2 Answers2

0

Using str to access the column 3rd position value

data["info_trig"].str[3]
BENY
  • 317,841
  • 20
  • 164
  • 234
0
data.apply(lambda x: x['cal_nCa'] if x['info_trig'][1] == 0 else 0, axis = 1)

This will return a Series that only remain value in cal_nCa when the second element value in info_trig is 0:

0      0
1      0
2    116
3      0
dtype: int64

Or you can only select the rows you want by this:

data[data.apply(lambda x: True if x['info_trig'][1] == 0 else False, axis = 1)]

Hope it will help you.

Ting
  • 3
  • 2