Home > OS >  Problem by using for loop when loading data series from .csv file and save them as array matrix for
Problem by using for loop when loading data series from .csv file and save them as array matrix for

Time:04-29

I want to load a series of data from 6 CSV files and save them per column of the data series. As I call the Column_A, Column_B and new_Column_A, only the last output array of the data series which is the 6th CSV file is saved. Is there a possible way or function that could make me save the array of every data series of every CSV file at the end of the for loop?

Below is the code that I have so far:

for n in range(1,7):
    data = np.genfromtxt('data'   str(n)   '_.csv', delimiter=",")

    Column_A = data[35, :]
    Column_A = np.flip(Column_A ) 
    Column_A = np.reshape(Column_A , (len(Column_A )))

    new_Column_A = np.cumsum(Column_A )

    Column_B = data[0, :]
    Column_B = np.flip(Column_B ) # Reverse Array
    Column_B  = np.reshape(Column_B , (len(Column_B )))

print(Column_A)
print(Column_B)
print(new_Column_A)

CodePudding user response:

I suggest you to using pandas if you can, it is very powerful and simple for managing this kind of operation.

Here you can find some examples of csv merging by using pandas: How to read all csv files in a folder in pandas?

CodePudding user response:

I also strongly recommend you use pandas. It's convenient unless your csv file is tremendous. You can use

pd.read_csv

to load 6 csv files then concatenate them into a new file.

this link may help. https://pandas.pydata.org/docs/reference/index.html

  • Related