gpt4 book ai didi

python - for循环不适用于python中的空数据框

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

我有一个 python df:

    def foo():
s1 = pd.DataFrame({'A' : []})
s2 = pd.DataFrame({'B' : [2]})
s3 = pd.DataFrame({'C' : [3]})
return s1, s2, s3

s1, s2, s3 = foo()

s1 为空,s2 返回 2,s3 返回 3。

我写了这个for循环:

    s = [s1, s2, s3]
for i in s:
if i.empty:
print(i + "the result is null")
else:
print(i + "the result is not null")

但这不是我想要的:

    s1 the result is null
s2 the result is not null
s3 the result is not null

有什么建议吗???

最佳答案

一旦将它们放入列表中,您就会丢失这些名称..

这个怎么样:

s = {'s1':s1,'s2':s2,'s3':s3}
for i in s.keys():
if s[i].empty:
print(i + " the result is null")
else:
print(i + " the result is not null")

关于python - for循环不适用于python中的空数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59815837/

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