简体   繁体   中英

selecting from a pandas.dataframe based on a column of arrays

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.

使用str访问列第3个位置值

data["info_trig"].str[3]
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM