Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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 for循环的输出有问题吗?_Python_Pandas - Fatal编程技术网

Python for循环的输出有问题吗?

Python for循环的输出有问题吗?,python,pandas,Python,Pandas,我试图写一段代码,证明两个匿名列显示的信息不相同。我最初的假设是,A4='u',A5将等于'g' 运行这个for循环后,输出给我一堆其他变量对,即“u”与“p”或“gg”成对(这些是A5的其他值): for row4 in crx_data['A4']: for row5 in crx_data['A5']: if ((row4 == 'u') & (row5 != 'g')): print(row4, row5) 因此,我反驳了我的假设

我试图写一段代码,证明两个匿名列显示的信息不相同。我最初的假设是,
A4
='u',
A5
将等于'g'

运行这个for循环后,输出给我一堆其他变量对,即“u”与“p”或“gg”成对(这些是
A5
的其他值):

for row4 in crx_data['A4']:
    for row5 in crx_data['A5']:
        if ((row4 == 'u') & (row5 != 'g')):
            print(row4, row5)
因此,我反驳了我的假设,但我希望能够在我的工作中更好地展示这一点。目前,这个for循环打印了成吨的变量对,这些变量对与“u”==“g”模式相矛盾


我如何修改for循环,使其显示的次数与实际显示的次数相同,以便我可以计算值来反驳我的假设?

这给出了“u”和“g”在一起的次数以及其他情况下的次数

count_together = 0
count_not_together = 0
for row4 in crx_data['A4']:
    for row5 in crx_data['A5']:
        if ((row4 == 'u') and (row5 == 'g')):
            count_together = count_together + 1
        else:
            count_not_together = count_not_together + 1
print('Count together : ',str(count_together))
print('Count not together : ',str(count_not_together))
在代码中,您使用了位运算符“&”而不是关键字“and”。

这给出了“u”和“g”在一起的次数以及其他情况下的次数

count_together = 0
count_not_together = 0
for row4 in crx_data['A4']:
    for row5 in crx_data['A5']:
        if ((row4 == 'u') and (row5 == 'g')):
            count_together = count_together + 1
        else:
            count_not_together = count_not_together + 1
print('Count together : ',str(count_together))
print('Count not together : ',str(count_not_together))
在代码中,您使用了位运算符“&”而不是关键字“and”。 IIUC:您可以使用
np.where
生成掩码,以过滤
A4
u
A5
不是
g
的列。 您可以使用以下选项:

或者

IIUC:您可以使用
np.where
生成掩码,以过滤
A4
u
A5
不是
g
的列。 您可以使用以下选项:

或者


对于逻辑条件,应使用关键字
&
是通过数据帧的“按位and”运算符DONT循环,除非绝对necessary@IainShelvington不在Pandas中。您应该在逻辑条件中使用关键字
&
是“按位and”运算符不通过数据帧循环,除非绝对necessary@IainShelvington不是在熊猫身上。
result = crx_data[["A4", "A5"]][(crx_data["A4"] == "u") & (crx_data["A5"] != "g")]
print(result)
print("Number of times such pairs appear:", len(result))