I want to apply all the files in the folder as a for statement.

Asked 1 weeks ago, Updated 1 weeks ago, 12 views

Hello, my code is as follows.

import os
file_list = os.listdir ('path with file')
file_list
# Save the file name you want to run in file_list (csv file)
for i in file_list:
    data = pd.read_csv (where f'file is located/{i}')
    max = data[['Screw Velocity','Eject Pos', 'Screw Pos', 'Back Flow', 'Extrude Torque', 'Inject             Pressure']].max()
    min = data[['Screw Velocity','Eject Pos', 'Screw Pos', 'Back Flow', 'Extrude Torque', 'Inject Pressure']].min()
    mean = data[['Screw Velocity','Eject Pos', 'Screw Pos', 'Back Flow', 'Extrude Torque', 'Inject Pressure']].mean()
    std = data[['Screw Velocity','Eject Pos', 'Screw Pos', 'Back Flow', 'Extrude Torque', 'Inject Pressure']].std()
    df = pd.DataFrame([max,min,mean,std])
    df_values = df.values
    datalist=[]
    datalist.append(df_values)
    data_array = np.array(datalist)
    data_reshape = data_array.reshape(1,24)
    c_file = pd.DataFrame(data_reshape)

# So far, it's a code that extracts statistics from the columns I want from one file and connects them to one row.

c_file = pd.DataFrame(data_reshape)
file_path = os.path.join ('path you want to save + file name differently', file)
c_file.to_csv(file_path)
# So far, it's a code that stores different file names.

When I run it, a total of three files need to be created, but only one file in the last order is created in file_list. ex) If you type file_list=[1.csv, 2.csv, 3.csv], only the last 3.csv file is created. Help me!

python

2022-09-19 23:21

1 Answers

From the current code

c_file = pd.DataFrame(data_reshape)
file_path = os.path.join ('path you want to save + file name differently', file)
c_file.to_csv(file_path)
# So far, it's a code that stores different file names.

This part seems to be working outside the for statement. Why don't you make it work inside the for door?


2022-09-19 23:21

If you have any answers or tips


© 2022 pinfo. All rights reserved.