简体   繁体   English

从基于数组列的pandas.dataframe中进行选择

[英]selecting from a pandas.dataframe based on a column of arrays

I have a data frame with a column containing arrays (all 1x9 arrays). 我有一个数据框,其中包含一个包含数组的列(所有1x9数组)。 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. 对于该列中的所有行,我希望找到第三个元素为1的那些行,并从相应行中的另一列中选择值。 For example, I wish to pick out the 'cal_nCa' value (116) where the second element in info_trig is 0 例如,我希望选择'cal_nCa'值(116),其中info_trig中的第二个元素是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 . 我在df["A"][(df["B"] > 50)]基础上尝试了一些基于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 : 这将返回一个系列,仅停留在价值cal_nCa当第二个元素值info_trig0

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. 希望它会对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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