for循环在python中不使用空数据帧
我有一个python df:for循环在python中不使用空数据帧,python,Python,我有一个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 我为循环写了这个: s = [s1, s2, s3] f
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
我为循环写了这个:
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")
一旦你把名字列入名单,你就会把它们弄丢 这个怎么样:
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")
s1、s2和s3只是引用特定数据帧对象的名称,但不是对象本身的一部分,因此无法打印 下面将打印第一个密钥的名称:
for i in s:
if i.empty:
print(i.keys()[0] + " the result is null")
else:
print(i .keys()[0] + " the result is not null")
给予
A the result is null
B the result is not null
C the result is not null
或者,您可以将名称存储在数据框本身。
for i in s:
if i.empty:
print(i.keys()[0] + " the result is null")
else:
print(i .keys()[0] + " the result is not null")
给予
A the result is null
B the result is not null
C the result is not null
或者,您可以将名称存储在数据帧本身。到底是什么问题?你不熟悉字典和列表吗?到底是什么问题?你不熟悉字典和列表吗?