How to add columns to an empty pandas dataframe?

  • A+

I have an empty dataframe.


for some reason I want to generate df2, another empty dataframe, with two columns 'a' and 'b'.

If I do


it does not work (I get the columns renamed to 'ab') and neither does the following


How to add a separate column 'b' to df, and df.emtpy keep on being True?

Using .loc is also not possible


as it returns

  Cannot set dataframe with no defined index and a scalar 

If you just do df['b'] = None then df.empty is still True and df is:

Empty DataFrame Columns: [a, b] Index: [] 

EDIT: To create an empty df2 from the columns of df and adding new columns, you can do:

df2 = pd.DataFrame(columns = df.columns.tolist() + ['b', 'c', 'd']) 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: