在数据框中填充多列

Filling multiple columns in dataframe

我有一个现有的数据框 df 作为:

df
                        KI       Date  
DateTime                                            
2019-12-01 01:00:00    42       2019-12-01
2019-12-01 02:00:00    42       2019-12-01

我想在创建新列时将以下 table 添加到上面的数据框中:

[[1, 2],[3, 4]]

最终答案如下

df
                        KI       Date       col1    col2
DateTime                                            
2019-12-01 01:00:00    42       2019-12-01  1       2
2019-12-01 02:00:00    42       2019-12-01  3       4

我不知道该如何处理。

编辑:

[[1, 2],[3, 4]] is of type numpy.ndarray

尝试:

x=[[1,2], [3,4]]

pd.concat([df, pd.DataFrame(data=x, columns=["a", "b"])], axis=1, sort=True)

(将 ab 替换为您想要的列名)

你只需要pd.DataFrame

my_array = np.array([[1, 2],[3, 4]])
df[['col1','col2']] = pd.DataFrame(index=df.index,data = my_array)

                     KI        Date  col1  col2
DateTime                                       
2019-12-01_01:00:00  42  2019-12-01     1     2
2019-12-01_02:00:00  42  2019-12-01     3     4