python - 无法使用 For 循环将数据框的列名附加到列表中
问题描述
我有一个这样的数据框:-
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
现在我想在函数内部使用 for 附加包含“无”值的数据框的列
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
”lst
c
现在如果我在for loop
ie中打印
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']
解决方案
>>> df.columns[df.isna().any()].to_list()
['Full_2', 'Full_3', 'Full_4']
编辑:像这样更新你的函数。
def lister(df):
lst = []
for c in df.columns:
if (df[c].isna().max()) == True:
lst.append(c)
return lst
>>> lister(df)
['Full_2', 'Full_3', 'Full_4']
推荐阅读
- android - getGeoPoint() 方法在调用时返回 null。Firebase 集合中有超过 1 个文档
- javascript - 当有人加入服务器时,试图让 Discord 机器人写一条消息
- ios - 需要帮助制作 Swift Cookie Clicker 游戏自动保存
- sql - 使用日期分组
- android - 需要帮助从适配器 kotlin 打开 url
- java - 我如何在 firebase 中使用多个 wherelessthan?
- sql - 时间范围内的 Postgres SQL 归因
- r - 如何在 R 中设置.Seed 以在 Windows 操作系统上实现可重复性
- python - shuffle=True 如何影响具有两个输入的 Keras 模型?
- angular - 加载时的角度检查复选框