-1

I have a list with different csv files, what I would like to do is read these csv files and save in a variable for posterior operations.

This is what I have now


DF_list= list()

for filename in sorted(glob.glob(dirname + '/*.csv')):
    print(filename)
    df7 = pd.read_csv(filename)
    DF_list.append(df7)

And I want to make something like this, How can I do this?

df1 = pd.read_csv(DF_list[0])
df2 = pd.read_csv(DF_list[1])
df3 = pd.read_csv(DF_list[2])
>> ValueError: Invalid file path or buffer object type: <class 'pandas.core.frame.DataFrame'>

Thanks for your help!

coding
  • 917
  • 2
  • 12
  • 25

3 Answers3

0
  • DF_list is a list of DataFrames, not a list of CSV files
  • DF_list[0] is a DataFrame
  • df1 = pd.read_csv(DF_list[0]) tries to read a DataFrame as a CSV

to create multiple dataframes

df1 = DF_list[0]
df2 = DF_list[1]
df3 = DF_list[2]

to create one dataframe

df = pd.concat(DF_list)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0

You can create a dictionary and store data in each file in front of it's key. I don't know if this is helpful or not but This is just my suggestion.

import pandas as pd

DF_list = {}
a = ["Book1.csv", "Book2.csv"]

for i in a:
    DF_list[i] = []    

# for i in len(a):
    # DF_list[i] = []    

for filename in a:
    print(filename)
    df7 = pd.read_csv(filename)
    DF_list[filename] = df7

# print(DF_list["Book1.csv"]["column_name"])
print(DF_list["Book1.csv"])
# print(DF_list[0])
0

You can also do, using a list comprehension:

df=pd.concat([pd.read(file) for file in list])
zabop
  • 6,750
  • 3
  • 39
  • 84