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"]
CodePudding user response:
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 |