简体   繁体   English

单元格中的值是列表时的熊猫数据框条件

[英]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. AB列中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.

相关问题 如何根据条件在 Pandas dataframe 的单个单元格中列出 append 值 - How to append value to list in single cell in Pandas dataframe based on condition Pandas Dataframe 如果条件在单元格中设置值(元素在列表中) - Pandas Dataframe Set value in cell if condition (Element is in list) 如果在类型列表的单元格值中找到字符串,如何根据条件删除 pandas dataframe 中的行? - How to delete row in pandas dataframe based on condition if string is found in cell value of type list? 通过用列表替换单元格值来扩展pandas数据帧 - Expand pandas dataframe by replacing cell value with a list Pandas Dataframe - 根据正则表达式替换所有单元格值 - Pandas Dataframe - Replace all cell value subject to regex condition 如何根据条件为具有缺失值的 pandas dataframe 单元格分配另一个单元格的值? - How do I assign a value to a pandas dataframe cell with a missing value with the value of another cell based on a condition? 计算列表 pandas dataframe 的长度,其中单元格值为列表 - Calculate length of list pandas dataframe where cell value is a list 当一个单元格值等于特定字符串时,获取列表中的 pandas dataframe 列名 - Obtain the pandas dataframe column names in a list when one cell value equals to a specific string 熊猫:条件基于单元格中的列表 - Pandas: Condition based on list in a cell 按条件重复数据框中的单元格值 - Repeat the cell value in dataframe by condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM