Home > Mobile >  Slicing multiple CSV files at once
Slicing multiple CSV files at once

Time:10-10

I'm currently reading in 5 CSV files and looking to get a particular column of data for a different variables

I am doing that with Pandas read_csv, and then trying to slice that variable

all_files = pd.read_csv('ca.csv'), pd.read_csv("ny.csv"), pd.read_csv("tn.csv"), pd.read_csv('az.csv'), pd.read_csv("tx.csv')

date_index = all_files[1:, 1]

This throws up error TypeError: tuple indices must be integers or slices, not tuple

Not sure where I'm going wrong here?

CodePudding user response:

The cause of the error is thatall_files is a tuple of several dataframes, one from each file that you read. So when you try to do all_files[slice] you're slicing the tuple, and not the dataframes it contains. Tuples can only be sliced using integers, just like a plain list, hence the error.

If you want to get the same column from each of the dataframes, you need some loop structure:

date_indexes = [df.ix[:,0] for df in all_files]

The above code for example will get you the 1st column from each of the dataframes you read, as a series, in a list.

If your intention is to combine the dataframes in some way after you read them, you might not need this. You could instead look into stacking them. Stacking also works on pandas series (which is what the extracted columns from the dataframes are), if what you want is to get a long sequence by combining the same column from each file.

  • Related