gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:22 25 4
gpt4 key购买 nike

我有一个主 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com