PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How can I append a matrix to a matrix in python?

 May 12, 2022     append, matrix, python     No comments   

Issue

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


Solution

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.



Answered By - Hasanen A. Sahib
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing