python pandas在dataframe列中添加列表作为默认值

时间:2018-08-06 21:38:09

标签: python pandas

我想在数据框df中创建一个新列,该列将用[np.nan]填充所有行

    df['new'] = [np.nan]

我知道

ValueError: Length of values does not match length of index

如果我尝试

    test['new'] = np.nan
    test['new'] = test['new'].astype('object')
    test['new'] = [np.nan]

我明白了

ValueError: Length of values does not match length of index

我想确保所有行都填充有包含nan

的列表

1 个答案:

答案 0 :(得分:2)

您已分配了object

df['New']=[[np.nan]]*len(df)
df
Out[250]: 
        Date  Value    New
0 2017-01-01      1  [nan]
1 2017-02-13      2  [nan]
2 2018-03-01      3  [nan]
3 2018-04-01      4  [nan]