0

I have 12 csv files which I wanted to import into a data frame in column wise.

For instance, the each 12 csv files are named differently as follows:

filenames = ['experiment_timesteps_1.csv',
             'experiment_timesteps_2.csv',
             'experiment_timesteps_3.csv',
             'experiment_timesteps_4.csv',
             'experiment_timesteps_5.csv',
             'experiment_timesteps_6.csv',
             'experiment_timesteps_8.csv',
             'experiment_timesteps_10.csv',
             'experiment_timesteps_12.csv',
             'experiment_timesteps_15.csv',
             'experiment_timesteps_18.csv',
             'experiment_timesteps_20.csv']

I wanted to use the unique file names (11th to the last 4) as column header in a new data frame. Each file consists of a single column with the same number of rows as follows:

results
266430.1827
318881.2395
285411.9195
279878.2699
272394.9219
239213.2243
274932.4677
290705.0974
315464.9616
240384.0452

I encountered the error with the execution of the following code:

results = DataFrame()
for name in filenames:
    results[name[11:-4]] = read_csv(name, header=0)

ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series

1 Answer 1

1

If I understood correctly, you can do it as follows:

results = DataFrame()
for name in filenames:
    aux = read_csv(name)
    results[name[11:-4]] = aux["results"]

This will generate a column for each file with the unique identifier you want and the "results" column of each csv will be saved in the corresponding column.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.