简体   繁体   中英

Pandas dataframe condition when value in cell is a list

How would I find the value of a cell in column x on the condition that the value of column B in the same row is the list [0,1] ?

Each cell in columns A and B are lists of integers.

               x      y       A       B 
0           ding   ding    [0, 1]  [0, 0] 
1           ding   dong    [0, 1]  [0, 1] 
2           ding   ding    [0, 1]  [0, 0] 
3           ding   ding    [0, 1]  [0, 0]  
4           ding   dong    [0, 1]  [0, 1] 
5           ding   ding    [0, 1]  [0, 0] 
6           ding   ding    [0, 1]  [0, 0] 

IIUC you can create Series with same index as DataFrame and compare it with column B :

s = pd.Series([[0, 1]], index = df.index)
print s
0    [0, 1]
1    [0, 1]
2    [0, 1]
3    [0, 1]
4    [0, 1]
5    [0, 1]
6    [0, 1]
dtype: object

print df['B'] == s
0    False
1     True
2    False
3    False
4     True
5    False
6    False
dtype: bool

print df[df['B'] == s]
      x     y       A       B
1  ding  dong  [0, 1]  [0, 1]
4  ding  dong  [0, 1]  [0, 1]

print df.x[df['B'] == s]
1    ding
4    ding
Name: x, dtype: object

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