首页 > 解决方案 > 在python中将数组添加到现有数据框

问题描述

我有一个像

array([[-8.76297433e-01],
       [-1.05157165e+00],
       [ 9.97287956e-02],
       [ 9.97287956e-02],
       [-1.22684587e+00],
       [-1.22684587e+00],
       [-1.22684587e+00],
       [-8.76297433e-01]])

我想将此作为一列添加到与数组行数相同的现有数据框中。

标签: pythonpandas

解决方案


import numpy as np
import pandas as pd

# your array
a = np.array([[-8.76297433e-01],
              [-1.05157165e+00],
              [ 9.97287956e-02],
              [ 9.97287956e-02],
              [-1.22684587e+00],
              [-1.22684587e+00],
              [-1.22684587e+00],
              [-8.76297433e-01]])

# your dataframe
df = pd.DataFrame(a, columns=['col1'])
df

# add columns
df['col2'] = a

# show the result
df

推荐阅读