Select Dataframe by Column and give np.NaN for the missing columns

Question:

I have a dataframe:

df = pd.DataFrame([{"a": 1, "b": 2}])

I want to create another dataframe with column "a" and "c" and give np.NaN for column "c". I have something like this, but it throws error since "c" is not in df. What is a clean and working way to do it?

df1 = df["a","c"]
Asked By: topcan5

||

Answers:

You can use pandas.DataFrame.join to add the "c" column with NaN values to your existing dataframe like this:

import pandas as pd
import numpy as np

df = pd.DataFrame([{"a": 1, "b": 2}])

df = df.join(pd.DataFrame(columns=["c"]))

display(df)

The output:

index a b c
0 1 2 NaN
Answered By: Kim Tang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.