简体   繁体   中英

How can I append a matrix to a matrix in python?

I was wondering how I can append a selected part of a matrix to a matrix in python? The link below shows the excel file data that I am using and am trying to append with:

enter image description here

My current program is shown below:

import pandas as pd
main=pd.read_csv('C:/Users/Jonas/Desktop/testfile/testing.csv', header=None)
main_transposed=main.T
#Next excel file
wave_file=pd.read_csv('C:/Users/Jonas/Desktop/testfile/nxt_Test.csv', header=None)
wave_transposed=wave_file.T

Thanks

Firstly, the best for matrix problems is the Numpy library, so convert your DataFrame to ndarray (simply) then use the bellow concept of collecting parts of the matrix:

import numpy as np
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
b=np.array([[10,12,11],[100,5,79]])
b1_above_a=np.reshape(np.append(b[1,:],a),(4,3))
a_above_b1=np.reshape(np.append(a,b[1,:]),(4,3))

Now the b1_above_a is the second row of b put above matrix a, and the reverse with a_above_b1, that we put the whole matrix a above the second row of b. Now keep in mind the output will be flattened so reshape it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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