我本来以为你的语法也可以工作。问题出现了,因为当你用列列表语法(
df[[new1, new2]] = ...
)创建新的列时,pandas要求右侧是一个DataFrame(注意,如果DataFrame的列与你所创建的列有相同的名字,实际上并不重要)。
你的语法对于将标量值分配给
existing
列,而pandas也很乐意使用单列语法(
df[new1] = ...
)将标量值分配给新的列。因此,解决方案是要么将其转换为几个单列赋值,要么为右侧创建一个合适的DataFrame。
这里有几种方法可以
will
work:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'col_1': [0, 1, 2, 3],
'col_2': [4, 5, 6, 7]
Then one of the following:
1) Three assignments in one, using list unpacking:
df['column_new_1'], df['column_new_2'], df['column_new_3'] = [np.nan, 'dogs', 3]
2) DataFrame conveniently expands a single row to match the index, so you can do this:
df[['column_new_1', 'column_new_2', 'column_new_3']] = pd.DataFrame([[np.nan, 'dogs', 3]], index=df.index)
3) Make a temporary data frame with new columns, then combine with the original data frame later:
df = pd.concat(
pd.DataFrame(
[[np.nan, 'dogs', 3]],
index=df.index,
columns=['column_new_1', 'column_new_2', 'column_new_3']
], axis=1
4) Similar to the previous, but using join instead of concat (may be less efficient):
df = df.join(pd.DataFrame(
[[np.nan, 'dogs', 3]],
index=df.index,
columns=['column_new_1', 'column_new_2', 'column_new_3']
5) Using a dict is a more "natural" way to create the new data frame than the previous two, but the new columns will be sorted alphabetically (at least before Python 3.6 or 3.7):
df = df.join(pd.DataFrame(
'column_new_1': np.nan,
'column_new_2': 'dogs',
'column_new_3': 3
}, index=df.index
6) Use .assign() with multiple column arguments.
我非常喜欢@zero的这个答案的变体,但是和前面的一样,新的列总是按字母顺序排序,至少在早期的Python版本中是这样。
df = df.assign(column_new_1=np.nan, column_new_2='dogs', column_new_3=3)
new_cols = ['column_new_1', 'column_new_2', 'column_new_3']
new_vals = [np.nan, 'dogs', 3]
df = df.reindex(columns=df.columns.tolist() + new_cols) # add empty cols
df[new_cols] = new_vals # multi-column assignment works for existing cols
8) In the end it's hard to beat three separate assignments:
df['column_new_1'] = np.nan
df['column_new_2'] = 'dogs'
df['column_new_3'] = 3
注意:这些选项中有许多已经在其他答案中涉及。向DataFrame添加多列并将其设置为与现有列相等的列, 是否可以在pandas DataFrame中一次性添加几列?, 为pandas数据框架添加多个空列