I'm looking to read multiple csv file, and then save them into separate dataframe names.
path = os.getcwd()
csv_files = glob.glob(os.path.join(r'blabla/Data', "*.csv" ))
for f in csv_files:
df = pd.read_csv(f)
print('Location:', f)
print('File Name:', f.split("\\")[-1])
print('Content:')
df.pop('Unnamed: 0')
display(df)
print
When I display(df) within the loop, it displays all three tables in the 3 csv files in that folder. However, when I print df outside the loop, it only gives me the last table that was generated. How do I save each table from the csv file into separate data frames?
CodePudding user response:
seems like you're overwriting the same variable again and again
path = os.getcwd()
csv_files = glob.glob(os.path.join(r'blabla/Data', "*.csv" ))
list_of_dfs = []
for f in csv_files:
df = pd.read_csv(f)
print('Location:', f)
print('File Name:', f.split("\\")[-1])
print('Content:')
df.pop('Unnamed: 0')
display(df)
list_of_dfs.append(df)
access the individual dataframes with list_of_dfs[0]
, list_of_dfs[1]
,...