In [1]:
import pandas as pd
In [11]:
df = pd.DataFrame([
{
'a': 1,
'b': 2,
'd': 4
}
])
In [12]:
df
Out[12]:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html
In [14]:
columns = ['a', 'b', 'c', 'd']
In [16]:
df.reindex(columns=columns, fill_value=0)
Out[16]:
This can also be used to get a subset of the columns
In [21]:
columns_subset = columns[:2]
columns_subset
Out[21]:
In [19]:
df.reindex(columns=columns_subset, fill_value=0)
Out[19]:
Which is probably better done this way
In [20]:
df[columns_subset]
Out[20]: