How can make a dataset of elements of matrices in dataframe?

后端 未结 1 786
无人及你
无人及你 2020-12-07 06:24

I have dataset of 3 parameters \'A\',\'B\',\'C\' in .TXT file and after I print them in 24x20 matrices I need to collect

相关标签:
1条回答
  • 2020-12-07 06:39

    I am not sure if I understood your question fully but this is a solution:

    Convert your data frame to a 2d numpy array using as_matrix() then use ravel() to get a vector of size 480 * 3 then cycle over your cycles and use vstack method for stacking rows over each other in your result, this is a code with your example data:

    A = [[1,2,3,4], [10,20,30,40]]
    B = [[4,5,6,7], [40,50,60,70]]
    C = [[8,9,10,11], [80,90,100,110]]
    
    cycles = 2
    
    for cycle in range(cycles):
        data = {'A': A[cycle], 'B': B[cycle], 'C': C[cycle]}
        df = pd.DataFrame(data)
        D = df.as_matrix().ravel()
        if cycle == 0:
            Results = np.array(D)
        else:
            Results = np.vstack((Results, D2))
    # Output: Results= array([[  1,   4,   8,   2,   5,   9,   3,   6,  10,   4,   7,  11], [ 10,  40,  80,  20,  50,  90,  30,  60, 100,  40,  70, 110]], dtype=int64)
    np.savetxt("Results.csv", Results, delimiter=",")
    

    Is this what you wanted?

    0 讨论(0)
提交回复
热议问题