python - 在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]])
我想将此作为一列添加到与数组行数相同的现有数据框中。
解决方案
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
推荐阅读
- c# - MySql 连接。值不能为空。参数名称:类型
- image - 将图像转换为 URL
- php - 带有 imagepng 错误输出的德语变音符号
- django - DRF:可在序列化程序选择字段“选择”中调用
- javascript - 触摸移动在 30-80 次后卡住并出现控制台错误
- python - Python 计数器值和 boolean all() 未按预期工作
- c++ - C++ 编译器不喜欢“使用 INLINE = extern inline”
- java - 如何使用 JAVA 泛型从 POJO 类对象中获取(int)值
- node.js - 在布尔查询中匹配条件的记录数
- r - 如何从r中的多个列表追加到列表?