How to add an empty column to a dataframe?

后端 未结 11 663
自闭症患者
自闭症患者 2020-11-28 01:06

What\'s the easiest way to add an empty column to a pandas DataFrame object? The best I\'ve stumbled upon is something like

df[\'foo\'] = df.ap         


        
11条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 01:35

    I like:

    df['new'] = pd.Series(dtype='your_required_dtype')
    

    If you have an empty dataframe, this solution makes sure that no new row containing only NaN is added.

    Specifying dtype is not strictly necessary, however newer Pandas versions produce a DeprecationWarning if not specified.

提交回复
热议问题