How to add an empty column to a dataframe?

后端 未结 11 637
自闭症患者
自闭症患者 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:23

    If I understand correctly, assignment should fill:

    >>> import numpy as np
    >>> import pandas as pd
    >>> df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4]})
    >>> df
       A  B
    0  1  2
    1  2  3
    2  3  4
    >>> df["C"] = ""
    >>> df["D"] = np.nan
    >>> df
       A  B C   D
    0  1  2   NaN
    1  2  3   NaN
    2  3  4   NaN
    

提交回复
热议问题