Generate a python array of multiple variables

Asked

Viewed 81 times

0

Hello! I am trying to generate a function that reads imported data from a "csv" file and converts it to an array. The data of one column is integer, and of the other column is an intercalation of strings and integer, thus:

inserir a descrição da imagem aqui

What I’ve written so far is: "

def sundata(arquivo):
import pandas as pd
import numpy as np

s_data = pd.read_csv(arquivo, delimiter = ';')
par_dat = []


for i,j in zip( par_dat['Az'] , par_dat['data'] ):

    par_data = np.append(par_dat,[[i],[j]])


    par_dat= np.reshape(par_dat,(len(data['Az']),2))

return par_dat

" What I get is the following error: "List indices must be integers or Slices, not strings". If anyone can help I would be very grateful :)

  • Why would you do that? Pandas wouldn’t be enough to help you do any operation with this data? Is there any restriction?

  • I need to use a function within a library called Pymag. In the function, I enter a series of data and it returns an integer value. If I had little data I could make a list or insert one by one. But it turns out that I have more than 1000 data and make each of them unfeasible. So I’m trying to create a way to insert them all at once into the function and then create a txt of the answer of each data.So I’m trying to import the data from csv and turn it into an array"...

  • accurate preserves the name of columns?

  • No, there’s no need!

1 answer

0


In the pandas themselves you have a method called values that converts the dataframe into a array.

def sundata(arquivo):
    import pandas as pd
    import numpy as np

    s_data = pd.read_csv(arquivo, delimiter = ';')
    par_dat = s_data.values

    return par_dat

Another option would be to use another pandas method to_numpy, that converts the dataframe into a numpy.array

def sundata(arquivo):
    import pandas as pd
    import numpy as np

    s_data = pd.read_csv(arquivo, delimiter = ';')
    par_dat = s_data.to_numpy()

    return par_dat

Browser other questions tagged

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