I have a list that contains a list of target columns:
cols = ["col1", "col2", "col4"]
Then I have several pandas DataFrames with a different number of columns. I must select columns from cols
. If one of the columns from cols
does not exist in a DataFrame, then NaN values should be generated.
df1 =
col1 col3
1 x1
2 x2
3 x3
df2 =
col1 col2 col4
1 f1 car3
3 f2 car2
4 f5 car1
For example, df2[cols]
works well, but df1[cols]
obvioulsy fails. I need the following output for df1
df1 =
col1 col2 col3
1 NaN NaN
2 NaN NaN
3 NaN NaN