如何添加具有固定值的多个新列?

我需要向DataFrame df添加列.应该沿着df的所有行修复所有新列的值:

df = {
       "NUM":[1,2],
       "WAKE":["M","H"],
       "DISTANCE":[780,500]
     }

new_df = pd.DataFrame(df)

这就是我尝试添加具有固定值的新多列的方法.

for column, row in new_df.iterrows():
    row["TEMPERATURE"] = 20
    row["VISIBILITY"] = 5000
    row["WIND"] = 10

此代码不会失败,但不会创建新列.

预期结果:

NUM   WAKE  DISTANCE  TEMPERATURE  VISIBILITY  WIND
1     M     780       20           5000        10
2     H     500       20           5000        10

最佳答案 这与使用字典的单个assign调用一样简单:

dct = {'TEMPERATURE': 20, 'VISIBILITY': 5000, 'WIND': 10}
new_df2 = new_df.assign(**dct)
new_df2

   NUM WAKE  DISTANCE  TEMPERATURE  VISIBILITY  WIND
0    1    M       780           20        5000    10
1    2    H       500           20        5000    10
点赞