I would like to append a column of zeros to a dataframe if the column in question is not already inside the dataframe.
If the dataframe looks like this:
df = pd.DataFrame({'a':[0,1,0], 'c':[1,1,1]})
----------------------------------------------
a c
0 0 1
1 1 1
2 0 1
And the complete list of column names that the dataframe should have are:
col_names = ['a', 'b', 'c']
I would like the output to look like this after applying the list comprehension to the df:
a b c
0 0 0 1
1 1 0 1
2 0 0 1
This is the complete code I have so far:
col_names = ['a','b','c']
df = pd.DataFrame({'a':[0,1,0], 'c':[1,1,1]})
# This is what I would like to convert into a list comprehension (one line) format if possible
for col in col_names:
if col not in df.columns:
df[col] = 0
# Re-order the columns into the correct order
df = df[col_names]
print(df)
CodePudding user response:
A list comprehension would produce a list. You don't want a list, you want to add columns to your dataframe. List comprehensions should not be used for side effects, ever.
You can however, produce the columns as a list and use advanced indexing to assign all the columns at the same time:
df[[col for col in col_names if col not in df.columns]] = 0