How to add an array inside a python Matrix

Asked

Viewed 180 times

1

Hello I’m having a problem in python I’m taking an excel a Matrix of 15 columns and 3 rows, so I’m reading excel with pandas and did that while below to read all columns and all rows:

import pandas as pd
import numpy as np
from collections import Counter
from pandas import ExcelWriter
from pandas import ExcelFile
from openpyxl import load_workbook

loto = pd.read_excel(r'lotofacil2.xlsx')

numlinesandcols = loto.shape

print(numlinesandcols)

cont = 0
matrix = []
matrix2 = []
line = []
contcolum = 1
contline = 0

while(contcolum != 16 and contline <= 2):
    colunm = 'Bola' + str(contcolum)
    valuenumber = loto.loc[contline, colunm]
    int(contcolum)
    print(contline, contcolum)
    line.append(valuenumber)
    contcolum = contcolum + 1
    print(colunm)
    print(valuenumber)
    if contcolum == 16 and contline <= 2:
        print(line)
        print('entrou no IF')
        matrix.append(line)
        # np.concatenate((matrix, line))
        print(matrix)
        line.clear()
        print(line)
        contcolum = contcolum - 15
        contline = contline + 1
        tamanhoMatrix = len(matrix)
        print(tamanhoMatrix)
else:
    print('fim do código')


print(matrix2)

But when he prints the Matrix instead of returning me this result:

[[18, 20, 25, 23, 10, 11, 24, 14, 6, 2, 13, 9, 5, 16, 3], [23, 15, 5, 4, 12, 16, 20, 6, 11, 19, 24, 1, 9, 13, 7], [20, 23, 12, 8, 6, 1, 7, 11, 14, 4, 16, 10, 9, 17, 24]]

He’s returning me this result:

[[23, 12, 8, 6, 1, 7, 11, 14, 4, 16, 10, 9, 17, 24], [23, 12, 8, 6, 1, 7, 11, 14, 4, 16, 10, 9, 17, 24], [23, 12, 8, 6, 1, 7, 11, 14, 4, 16, 10, 9, 17, 24]]

That is, it overwrites the previous arrays, has some way to concatenate the new array with the previous ones in Matrix?

  • I put all the code, I’m new to python so I couldn’t get a simpler solution, can you help me? And about the data not the 3 arrays of the right answer example, they should be inside an excel.

  • Joana and the archive lotofacil2.xlsx in order to test the program?

  • How do I add the file to the question?

  • 1

    Is it too big? You could put about three lines in the question and leave a link to the full file.

1 answer

0

That is, it overwrites the previous arrays, has some way to concatenate the new array with the previous ones in Matrix?

This is because you have created the while list objects. Therefore, with each while iteration, you are using the same list (line), and the Matix list is receiving multiple times the reference of the 'line list'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.