【发布时间】:2021-05-29 03:46:42
【问题描述】:
我有一个这样的数据框:-
data = [['a', 'b', 'c', 'd'],['q', 'r', 's', 't'],['n'],['w', 'x', 'y', 'z']]
df = pd.DataFrame(data, columns = ['Full_1', 'Full_2', 'Full_3', 'Full_4'])
现在我想在函数内使用 loop 附加包含“无”值的数据框的列
lst=[]
def lister(df):
for c in df.columns:
if (df[c].isna().max())==True:
lst.append(c)
return lst
else:
nope = 'None'
return nope
它返回“无”intsead of lst
现在如果我在for loop 的内部打印c,即
lst=[]
def lister(df):
for c in df.columns:
if (df[c].isna().max())==True:
print(c)
#return lst
else:
nope = 'None'
#return nope
c 在for 循环内的输出:-
Full_2
Full_3
Full_4
那么为什么这些值没有附加到名为 lst 的列表中?
lst的预期输出:-
['Full_2','Full_3','Full_4']
【问题讨论】:
标签: python pandas list for-loop