3

I have 4 csv files where I want to read simultaneously from the same folder, instead of reading them one by one. For instance, AAPL.csv, BABA.csv, PFE.csv and TSLA.csv. Which are actually stock quotes from yahoo finance. However, I am unable to assign each csv file to a dataframe respectively.

Below is my code:

    # import necessary libraries
    import pandas as pd
    import os
    import glob


    # use glob to get all the csv files 
    # in the folder
    path = os.getcwd()
    csv_files = glob.glob(os.path.join(path, "*.csv"))


    # loop over the list of csv files
    for f in csv_files:
  
    # read the csv file
    df = pd.read_csv(f)
  
    # print the location and filename
    print('Location:', f)
    print('File Name:', f.split("\\")[-1])
  
    # print the content
    print('Content:')
    print(df)

This is printed out:

 |Date | Open |High |Adj Close |
 | -------- | -------------- |-------------- |-------------- |
 | 2016-08-15     | 45.203999             |45.900002           |45.118000            |
 | 2016-08-16   | 45.098000            |45.438000            |44.722000            |

When I run the print(df), all the csv files are printed altogether. Is there a way where I can assigning one dataframe, for instance AAPL and print that out?

Same time, how to I only filter two columns of 'Date' and 'Adj Close' for all of the 4 stock quotes using a for loop?

Leonard
  • 53
  • 5

1 Answers1

1

You could assign each dataframe into a dictionary value, for example:

df_dict = dict()
for f in csv_files:
    df_dict[f] = pd.read_csv(f)

So you can retrieve the dataframe by doing:

df = df_dict[f]
Sprizgola
  • 416
  • 2
  • 10