[英]Python convert large numpy array to pandas dataframe
I have a chunk of code that I received that only works with pandas dataframes as input. 我收到的一大堆代码仅适用于pandas数据帧作为输入。 I currently have a pretty large numpy array.
我目前有一个很大的numpy数组。 I need to convert this into a pandas dataframe.
我需要将其转换为熊猫数据框。
The Dataframe will be 288 rows (289 counting the columns names) and 1801 columns. 数据框将为288行(计算列名称为289行)和1801列。 I have an array of size 1801 that will be all of the column names in the dataframe.
我有一个大小为1801的数组,它将是数据框中的所有列名。 Then I have an array of size (288) which will fill the first column.
然后我有一个大小为(288)的数组,它将填充第一列。 Then I have an array of shape (1800, 288) that will fill columns 2-1801.
然后我有一个形状数组(1800,288),它将填充列2-1801。 is there an easy way to turn this into a dataframe without individually defining all 1801 columns?
有没有一种简单的方法可以将其转换为数据帧而无需单独定义所有1801列?
I know I could define columns like column2=array[0,:], column3=array[1,:] but that will be alot of work for 1801 columns. 我知道我可以定义诸如column2 = array [0,:],column3 = array [1 ,:]之类的列,但这将为1801列做很多工作。
You can pass a numpy array directly to the DataFrame constructor: 您可以将numpy数组直接传递给DataFrame构造函数:
In [11]: a = np.random.rand(3, 5)
In [12]: a
Out[12]:
array([[ 0.46154984, 0.08813473, 0.57746049, 0.42924157, 0.34689139],
[ 0.29731858, 0.83300176, 0.15884604, 0.44753895, 0.56840054],
[ 0.02479636, 0.76544594, 0.24388046, 0.06679485, 0.94890838]])
In [13]: pd.DataFrame(a)
Out[13]:
0 1 2 3 4
0 0.461550 0.088135 0.577460 0.429242 0.346891
1 0.297319 0.833002 0.158846 0.447539 0.568401
2 0.024796 0.765446 0.243880 0.066795 0.948908
In [14]: pd.DataFrame(a.T)
Out[14]:
0 1 2
0 0.461550 0.297319 0.024796
1 0.088135 0.833002 0.765446
2 0.577460 0.158846 0.243880
3 0.429242 0.447539 0.066795
4 0.346891 0.568401 0.948908
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.