python - 如何遍历 df 的每一行/列以查看是否存在 0 或 ' '?

标签 python python-3.x pandas

我有一个主 df,我想遍历每一行和每一列,检查 0 和 ''。我需要输出它出现的行。麻烦的是,一个是int,另一个是str。因此,我为所有 int 列和 str 分别创建了子集调用 colsdf_string

我可以同时检查整个 dfstrint 还是我需要单独执行此操作?

这是我的代码:

cols = ['ID', 'Line Manager Id', 'Project ID']
df_strings = ['Email', 'Name', 'Department']
i = 0
k = 0
for i, j in df.iterrows():
    if df[cols[i]][j] == 0:
        print(i)
        print('incomplete')
    if df[df_strings[i]][j] == '':
        print(i)
        print('incomplete')

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

最佳答案

可以同时使用wherestack之后索引是包含0或空白的列

s=df.eq(0)|df.eq('')
df.where(df.eq(0)|df.eq('')).stack()

关于python - 如何遍历 df 的每一行/列以查看是否存在 0 或 ' '?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56212838/

相关文章:

python动态调用类

python - 值错误 : dictionary update sequence element #0 has length 1; 2 is required Python Dictionary

Python 表格抓取

Python Pandas 匹配两个索引和列的值

python - 测试不同列中值是否存在

python - 在程序关闭期间在 Python 中捕获 KeyboardInterrupt

python - 使用适用于 Python 的 Google API - 我从哪里获取 client_secrets.json 文件?

linux - keras导入失败

python - 使用套接字 API 编写基本的 HTTP 服务器

python - Pandas 值错误 : pattern contains no capture groups