【发布时间】:2015-09-04 18:13:45
【问题描述】:
如何将多个空列添加到列表中的DataFrame?
我能做到:
df["B"] = None
df["C"] = None
df["D"] = None
但我做不到:
df[["B", "C", "D"]] = None
KeyError: "['B' 'C' 'D'] not in index"
【问题讨论】:
-
None与 0 不同,但有些答案假设它是等价的。此外,分配None将给出对象的 dtype,但分配 0 将给出 int 的 dtype。 -
你也不能这样做
df[['B','C','D']] = None, None, None或[None, None, None]或pd.DataFrame([None, None, None])