[英]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]
? 在同一行中
B
列的值是列表[0,1]
的情况下,如何在x
列中找到单元格的值?
Each cell in columns A
and B
are lists of integers. A
和B
列中A
每个单元格都是整数列表 。
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
: IIUC您可以创建与
DataFrame
具有相同索引的Series
并将其与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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.