Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查数据框中是否存在两列中的值组合_Python_Sql_Pandas_Dataframe - Fatal编程技术网

Python 如何检查数据框中是否存在两列中的值组合

Python 如何检查数据框中是否存在两列中的值组合,python,sql,pandas,dataframe,Python,Sql,Pandas,Dataframe,我有两个数据帧。标题和章节是其中的两列。 我需要检查第二个数据帧中是否存在来自一个数据帧的特定标题和节的组合 例如,数据框torange具有列标题、s_低、s_高和其他usc有标题和部分列 如果torange中有以下行 title s_low s_high 1 1 17 代码需要签入usc如果是一行 title section 1 1 还有一排 title section 1 17 存在;并通过扩展usc中s_

我有两个数据帧。标题和章节是其中的两列。 我需要检查第二个数据帧中是否存在来自一个数据帧的特定标题和节的组合

例如,数据框
torange
具有列标题、s_低、s_高和其他<代码>usc有标题和部分列

如果
torange中有以下行

title   s_low   s_high
  1        1      17
代码需要签入
usc
如果是一行

title   section
 1        1
还有一排

title   section
 1          17
存在;并通过扩展
usc
中s_low和s_high之间的范围,创建一个新表,以写入
torange
中的标题、节和其余列

我已经写了下面的代码,但不知怎么的,它在几次迭代后就不起作用了。我怀疑“I”的计数器有问题,可能是语法错误。此外,它可能与
any()的语法有关。

(请忽略这些古怪的语句。这是我正在做的一项更大任务的一部分,打印出来的内容只是用来检查发生了什么。)


在这方面的任何帮助都将不胜感激。

您的整个检查和迭代都被搞砸了。您在
usc
中迭代
,但您的
any()
条件检查
usc
,而不是
。此外,
row
是两个循环的迭代器。以下是一些更清晰的起点:

for index, row in torange.iterrows(): 
    t = row['title']
    s_low = row['s_low']
    s_high = row['s_high']
    uscrow = usc[(usc.title == t) & (usc.section == slow)]
    # uscrow now contains all the rows in usc that fulfill your condition.

此外,在遍历数据帧时,我总是使用
iterrows()
iteritems()
,只是为了确保它确实做到了我期望的效果。实际上很少需要迭代。这是
isin
@FooBar的一个简单应用程序,感谢您的回复。我是熊猫队的新手,所以迭代过程很混乱。如何在同一声明/其他声明中检查s_high?我的结果集的行将同时满足以下条件[(usc.title==t)和(usc.section==s_low)]和[(usc.title==t)和(usc.section==s_high)]简单地说,特定标题的s_low和s_high都必须出现在usc中。@Jeff我如何在使用isin时进行迭代?我已经阅读了文档:如何将一些值作为参数传递给它?
for index, row in torange.iterrows(): 
    t = row['title']
    s_low = row['s_low']
    s_high = row['s_high']
    uscrow = usc[(usc.title == t) & (usc.section == slow)]
    # uscrow now contains all the rows in usc that fulfill your condition.