Use reindex for adding missing columns to a dataframe


In [1]:
import pandas as pd

In [11]:
df = pd.DataFrame([
    {
        'a': 1,
        'b': 2,
        'd': 4
    }
])

In [12]:
df


Out[12]:
a b d
0 1 2 4

In [14]:
columns = ['a', 'b', 'c', 'd']

In [16]:
df.reindex(columns=columns, fill_value=0)


Out[16]:
a b c d
0 1 2 0 4

This can also be used to get a subset of the columns


In [21]:
columns_subset = columns[:2]
columns_subset


Out[21]:
['a', 'b']

In [19]:
df.reindex(columns=columns_subset, fill_value=0)


Out[19]:
a b
0 1 2

Which is probably better done this way


In [20]:
df[columns_subset]


Out[20]:
a b
0 1 2