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?

3
  • 2
    Indentation matters in Python... Please show code with correct indentation. Commented Aug 16, 2021 at 9:04
  • Please refer stackoverflow.com/questions/55245721/… Commented Aug 16, 2021 at 9:07
  • in one line from pathlib import Path; df = pd.concat([pd.read_csv(f) for f in Path.cwd().glob('*.csv')]) lots of answers on this. Commented Aug 16, 2021 at 9:21

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.