Create columns in a pandas data frame with list comprehension

I have a list of variable names, var_list, and I want to make sure they exist in a pandas data frame. I achieved so with:

for cname in var_list:     if cname not in df.columns:         df[i]=0 

Is it possible to do the same with a list comprehension, like:

[df[i]=0 for i in var_list not if df.columns] 

or any other "more compact" way?

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.