简体   繁体   English

无法使用 For 循环将 append 的 Dataframe 的列名添加到列表中

[英]Can't append Column Names of Dataframe To A List Using For Loop

I have a Dataframe like this:-我有一个像这样的 Dataframe:-

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'])

Now I want to append the columns of a dataframe which contains 'None' value using for loop inside a function现在我想 append dataframe 的列,其中包含在 function 内使用 for 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

It returns me 'None' intsead of lst它返回我'无' intseadlst

Now If I print c Inside of for loop ie现在,如果我在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

Output of c inside for loop:- Output 的c内部for循环:-

Full_2
Full_3
Full_4

So Why these values not appending in list named lst?那么为什么这些值没有附加到名为 lst 的列表中呢?

Expected output of lst :- lst的预期 output :-

['Full_2','Full_3','Full_4']
>>> df.columns[df.isna().any()].to_list()
['Full_2', 'Full_3', 'Full_4']

Edit : update your function like this.编辑:像这样更新你的 function 。

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']

You're initialising the list every time.您每次都在初始化列表。

So it resets to an empty list inside the if statement.所以它在 if 语句中重置为一个空列表。

Move your lst=[] line outside of the for loop.lst=[]行移到 for 循环之外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用列名的情况下将列表追加到数据框? - How to append a list to dataframe without using column names? 我如何获取两个列表 Pandas DataFrame 列名,并且只使用一个列表,但 append 一个循环中的字符串应用于列名? - How can I take two lists of Pandas DataFrame columns names, and only use one list, but append a string in a loop to be applied to the column name? 如何从列表中为数据框的特定列名附加后缀 - How to append a suffix for specific column names of a dataframe from a list Append 列到 dataframe 使用列表理解格式 - Append column to a dataframe using list comprehension format 不能使用条件语句将 append 列值添加到列表中 - Can't append column values to a list using a conditional statement 如何在 for 循环中通过 for 循环将列表附加到数据帧 - How can i append a list to a dataframe via for loop in a for loop 如何使用名称列表更改 pandas Dataframe 中的列名称? - How to change column names in pandas Dataframe using a list of names? 如何使用for循环从Pandas DataFrame列进行追加? - How can I use a for loop to append from a Pandas DataFrame column? 如何使用Python将列表的值附加到DataFrame列的值? - How can I append the values of a list to the values of a DataFrame column using Python? 在pandas中使用to_dict()时,无法将列标题附加到数据框 - Can't append column headers to dataframe when using to_dict() in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM