从多个列表创建数据框

时间:2019-07-13 13:08:23

标签: python pandas numpy dataframe

我需要从列表的10个列表构建一个数据框。我是手动完成的,但需要一段时间。有什么更好的方法呢?

我尝试手动进行。它工作正常(#1) 我尝试使用代码(#2)以获得更好的性能,但它仅返回最后一列。

1

getDownloadUrl();
Task<Uri> urlTask = mUploadTask.continueWithTask(task -> {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }

                // Continue with the task to get the download URL
                return fileReference.getDownloadUrl();
            }).addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    if (downloadUri != null) {
                        // HERE IS YOUR DL LINK
                        String dl =  downloadUri.toString();
                    }

                } else {
                       // SOMETHING WENT WRONG 
                }
            });

    import pandas as pd
    import numpy as np
    a1T=[([7,8,9]),([10,11,12]),([13,14,15])]
    a2T=[([1,2,3]),([5,0,2]),([3,4,5])]
    print (a1T)
    #Output[[7, 8, 9], [10, 11, 12], [13, 14, 15]]
    vis1=np.array (a1T)
    vis_1_1=vis1.T
    tmp2=np.array (a2T)
    tmp_2_1=tmp2.T
    X=np.column_stack([vis_1_1, tmp_2_1])
    dataset_all = pd.DataFrame({"Visab1":X[:,0], "Visab2":X[:,1], "Visab3":X[:,2], "Temp1":X[:,3], "Temp2":X[:,4], "Temp3":X[:,5]})
    print (dataset_all)
    Output: Visab1  Visab2  Visab3  Temp1  Temp2  Temp3
0       7      10      13      1      5      3
1       8      11      14      2      0      4
2       9      12      15      3      2      5

> Actually I have varying number of columns in dataframe (500-1500), thats why I need auto generated column names. Extra index (1, 2, 3) after name Visab_, Temp_ and so on - constant for every case.  See code below.
For better perfomance I tried

code<br>
#2

n=3 # This is varying parameter. The parameter affects the number of columns in the table. m=2 # This is constant for every case. here is 2, because we have "Visab", "Temp" mlist=('Visab', 'Temp') nlist=[range(1, n)] for j in range (1,n): for i in range (1,m): col=i+(j-1)*n dataset_all=pd.DataFrame({mlist[j]+str(i):X[:, col]})

I expect output like

2 个答案:

答案 0 :(得分:0)

现在更清楚了。所以你有:

X=np.column_stack([vis_1_1, tmp_2_1])

让我们用列名创建一个列表:

columns_names = ["Visab1","Visab2","Visab3","Temp1","Temp2","Temp3"]

现在,您可以像这样直接制作一个数据框:

dataset_all = pd.DataFrame(X,columns=columns_names)

#Output
    Visab1  Visab2  Visab3  Temp1   Temp2   Temp3
0     7       10      13      1       5       3
1     8       11      14      2       0       4
2     9       12      15      3       2       5

答案 1 :(得分:0)

好,所以列数n是每个列表中子列表的数量,对吗?您可以用len来衡量:

len(a1T)
#Output
3

我将简化上面的答案,因此您不需要X并添加自动创建列名的方法:

my_lists = [a1T,a2T]
my_names = ["Visab","Temp"]

dfs=[]
for one_list,name in zip(my_lists,my_names):
  n_columns = len(one_list)
  col_names=[name+"_"+str(n) for n in range(n_columns)]
  df = pd.DataFrame(one_list).T
  df.columns = col_names
  dfs.append(df)

dataset_all = pd.concat(dfs,axis=1)

#Output
    Visab_0     Visab_1     Visab_2     Temp_0  Temp_1  Temp_2
0     7           10          13          1        5       3
1     8           11          14          2        0       4
2     9           12          15          3        2       5