1

This code generates the columns with missing's (it is ok)

for a in data.columns:
    if data[a].isnull().any() == True:
        print (a)

and now I want to use the columns of the previous code to apply to the following code

data[a].isnull().sum()

but the 'a' does not save the column names with missing's, is just the name of the last column. I need to use something like b=print (a) and doing the append do a list but I can´t find the right code.

5
  • Honestly this question isn't that clear, please consider editing it. If you need to save the items that trigger the conditional create something like a list then append to it. Commented Mar 27, 2018 at 12:33
  • I dont excatly understand what you want to do.. Get the number of empty collumns in data? Commented Mar 27, 2018 at 12:33
  • 1
    This is an XY problem. Don't ask about appending values to a list when in reality you want to do something else. If I understand the problem correctly, you want to count the NaN values in each column? Commented Mar 27, 2018 at 12:35
  • yes I want to get the number of empty columns in data but i just want to use the columns that are print in the first code, I don't want to see the others columns. I have this question on this code but I also want to understand how can I use the printing variables in other's codes because I need to use in more codes. Commented Mar 27, 2018 at 12:39
  • Thanks for clarifying, but I suggest you clean up your question and provide a minimal reproducible example. Show an example of a dataframe with some NaN values and the result you want to get. Commented Mar 27, 2018 at 12:42

2 Answers 2

1

Looks like you are looking for.

res = [data[a].isnull().sum() for a in data.columns]
print(res)
Sign up to request clarification or add additional context in comments.

Comments

1

This will provide a list of columns has nulls/NaNs

null_index = df.isnull().any()
a= null_index[null_index==True].index.tolist()

a will have a list of all the column names that has nulls in the column.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.