简体   繁体   English

迭代大熊猫中的值

[英]Iterate over values in pandas

i have a matrix in pandas 我在熊猫中有一个矩阵

print reducedMatrix
       0      1     3      4
    1  2  99991     0      0
    2  0      4     0      1
    3  3      0  9991      2
    4  1      0     2  99989

and want to iterate over it and print i and j for zero values, I use: 并希望迭代它并打印i和j为零值,我使用:

for i,rows in reducedMatrix.iterrows():
        for j,cols in reducedMatrix.iteritems():
            if (reducedMatrix[i][j] == 0): #here we check all zero values
                print i,j

but it does not work 但它不起作用

the expected output 预期的产出

1,3
1,4
2,0
2,3
3,0
4,1

It's essential to save row and col names 保存行和列名称至关重要

how do I achieve that? 我该如何实现?

Thanks in advance for any help! 在此先感谢您的帮助!

I would recommend using numpy's where function. 我会建议使用numpy的的where功能。

np.where(df.values == 0)

will return the indices of rows and columns that match the condition. 将返回与条件匹配的行和列的索引。 In order to retrieve the original row and column names: 为了检索原始的行和列名称:

rows, cols = np.where(df.values == 0)
list(zip(df.index[rows], df.columns[cols]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM