简体   繁体   English

从numpy数组创建熊猫数据框

[英]Create pandas dataframe from numpy array

To create a pandas dataframe from numpy I can use : 要从numpy创建一个熊猫数据框,我可以使用:

columns = ['1','2']
data = np.array([[1,2] , [1,5] , [2,3]])
df_1 = pd.DataFrame(data,columns=columns)
df_1

If I instead use : 如果我改为使用:

columns = ['1','2']
data = np.array([[1,2,2] , [1,5,3]])
df_1 = pd.DataFrame(data,columns=columns)
df_1

Where each array is a column of data. 其中每个数组都是一列数据。 But this throws error : 但这会引发错误:

ValueError: Wrong number of items passed 3, placement implies 2

Is there support in pandas in this data format or must I use the format in example 1 ? 熊猫是否支持这种数据格式,或者我必须使用示例1中的格式吗?

You need to transpose your numpy array: 您需要转置numpy数组:

df_1 = pd.DataFrame(data.T, columns=columns)

To see why this is necessary, consider the shape of your array: 要了解为什么这样做是必要的,请考虑数组的形状:

print(data.shape)

(2, 3)

The second number in the shape tuple, or the number of columns in the array, must be equal to the number of columns in your dataframe. 形状元组中的第二个数字或数组中的列数必须等于数据框中的列数。

When we transpose the array, the data and shape of the array are transposed, enabling it to be a passed into a dataframe with two columns: 当我们对数组进行转置时,将对数组的数据和形状进行转置,从而使其能够传递到具有两列的数据帧中:

print(data.T.shape)

(3, 2)

print(data.T)

[[1 1]
 [2 5]
 [2 3]]

DataFrames are inherently created in that order from an array. DataFrame是从数组中固有地按此顺序创建的。

Either way, you need to transpose something. 无论哪种方式,您都需要转置一些东西。

One option would be to specify the index=columns then transpose the whole thing. 一种选择是指定index = columns然后转置整个对象。 This will get you the same output. 这将为您提供相同的输出。

 columns = ['1','2']
 data = np.array([[1,2,2] , [1,5,3]])
 df_1 = pd.DataFrame(data, index=columns).T
 df_1

Passing in data.T as mentioned above is also perfectly acceptable (assuming the data is an ndarray type). 如上所述,传递data.T也是完全可以接受的(假设数据是ndarray类型)。

在第二种情况下,您可以使用:

df_1 = pd.DataFrame(dict(zip(columns, data)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM