pandas 将两个 numpy 数组转换为数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46379095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert two numpy array to dataframe
提问by Y.Z
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, )
我想将两个 numpy 数组转换为一个DataFrame
包含两列的数组。第一个 numpy 数组 'images' 的形状为102, 1024
。第二个 numpy 数组“标签”的形状(1020, )
My core code is:
我的核心代码是:
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)
What should I do to convert these two numpy array into two columns in one dataframe?
我应该怎么做才能将这两个 numpy 数组转换为一个数据框中的两列?
回答by MSeifert
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
.
您无法轻松堆叠它们,特别是如果您希望它们作为不同的列,因为您无法在 DataFrame 的一列中插入 2D 数组,因此您需要将其转换为其他内容,例如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'])
This will create a DataFrame
with 1020 rows and 2 columns, where each item in the second column contains 1D arrays of length 1024.
这将创建一个DataFrame
1020 行和 2 列,其中第二列中的每个项目包含长度为 1024 的一维数组。
回答by Yi Liu
columns=['label', 'images'] is extra
columns=['label', 'images'] 是额外的