将两个numpy数组转换为数据框 [英] Convert two numpy array to dataframe

查看:1142
本文介绍了将两个numpy数组转换为数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个numpy数组转换为一个包含两列的DataFrame. 第一个numpy数组'images'的形状为102, 1024. 第二个numpy数组标签"的形状为(1020, )

I want to convert two numpy array to one DataFrame containing two columns. The first numpy array 'images' is of shape 102, 1024. The second numpy array 'label' is of shape (1020, )

我的核心代码是:

images=np.array(images)
label=np.array(label)
l=np.array([images,label])
dataset=pd.DataFrame(l)

但事实证明这是一个错误:

But it turns out to be an error saying that:

ValueError: could not broadcast input array from shape (1020,1024) into shape (1020)

如何在一个数据帧中将这两个numpy数组转换为两列?

What should I do to convert these two numpy array into two columns in one dataframe?

推荐答案

您无法轻松堆叠它们,尤其是如果您希望将它们作为不同的列,因为您无法在DataFrame的一列中插入2D数组,因此您需要将其转换为其他内容,例如list.

You can't stack them easily, especially if you want them as different columns, because you can't insert a 2D array in one column of a DataFrame, so you need to convert it to something else, for example a list.

所以类似的事情会起作用:

So something like this would work:

import pandas as pd
import numpy as np
images = np.array(images)
label = np.array(label)
dataset = pd.DataFrame({'label': label, 'images': list(images)}, columns=['label', 'images'])

这将创建一个具有1020行和2列的DataFrame,其中第二列中的每个项目均包含长度为1024的一维数组.

This will create a DataFrame with 1020 rows and 2 columns, where each item in the second column contains 1D arrays of length 1024.

这篇关于将两个numpy数组转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆