Home>

I want to take all the .csv files from the folder and drive in this code, receiving correlations between files.

How to fix: (Symbols_List= Path (R'C: /DATA ')) ?

Import NUMPY AS NP
Import Pandas AS PD
Import Pandas_DataReader AS Web
From DateTime Import DateTime
Import Matplotlib.pyPlot AS PLT
Import seaborn.
Start= DateTime (2019, 1, 1)
symbols_list= ['aapl', 'f', 'twtr', 'fb', 'aal', 'amzn', 'googl', 'ge']
SYMBOLS= []
For Ticker in Symbols_List:
    R= Web.DataReader (Ticker, 'Yahoo', Start)
    R ['Symbol']= Ticker
    Symbols.APPEND (R)
DF= Pd.Concat (Symbols)
df= df.reset_index ()
DF= DF [['Date', 'Close', 'Symbol']]
df_pivot= df.pivot ('Date', 'Symbol', 'Close'). reset_index ()
Corr_DF= df_pivot.corr (Method= 'pearson')
cutr_df.head (). reset_index ()
Del Corr_Df.Index.name.
Corr_DF.

This is a fully working code, at the output:

Input data: https://dropmefiles.com/axaex.

Date ADJ Close Close High Low Open Volume
1/2/2018 166.353714 172.2599945 172.3000031 169.2599945 170.1600037 25555900
1/3/2018 166.3247223 172.2299957 174.5500031 171.9600067 172.5299988 29517900
1/4/2018 167.09729 173.0299988 173.4700012 172.0800018 172.5399933 22434600
1/8/2018 168.3720398 174.3500061 175.6100006 173.9299927 174.3500061 20567800
1/9/2018 168.3527222 174.3300018 175.0599976 173.4100037 174.5500031 21584000

Can you clarify -what is the essence of the question?

MaxU2021-06-01 12:27:31
  • Answer # 1

    If the essence of the question is to change this code so that the data is read from CSV files from one directory, then this can be done like this:

    Import Pandas AS PD
    From Pathlib Import Path
    P= Path (R "D: \ Download \ .data")
    DF= (PD
          .concat (
            [Pd.Read_CSV (F, UseCols= ["Date", "Close"]). Assign (Symbol= F.STEM)
             for f in p.glob ("*. CSV")]
            Ignore_Index= True)
          .pivot (index= "Date", columns= "symbol", values ​​= "close"))
    Corr_DF= df.corr (Method= 'Pearson')
    Pd.Options.display.float_format= '{:,. 4f}'. Format
    Print (Corr_DF)
    

    Result:

    Symbol ABBV ABC ABT ACAD ACG ACGL ACHC ACIW ACM ACN ADBE ADI ADM
    Symbol.
    ABBV 1.0000 0.5517 -0.8029 -0.2716 -0.8211 -0.6080 0.6024 -0.6606 0.0566 -0.5522 -0.6880 -0.6088 0.4409
    ABC 0.5517 1.0000 -0.3774 0.0644 -0.4869 -0.1198 0.4358 -0.4539 0.4234 -0.0658 -0.3704 -0.2713 0.1779
    ABT -0.8029 -0.3774 1.0000 0.6249 0.8910 0.8594 -0.6313 0.8973 0.2932 0.8414 0.8717 0.8102 -0.4257
    ACAD -0.2716 0.0644 0.6249 1.0000 0.5182 0.8295 -0.4231 0.5657 0.7552 0.7104 0.4519 0.6118 -0.4653
    ACC -0.8211 -0.4869 0.8910 0.5182 1.0000 0.7766 -0.6057 0.8677 0.1999 0.7411 0.8115 0.8170 -0.4128
    ACGL -0.6080 -0.1198 0.8594 0.8295 0.7766 1.0000 -0.4870 0.7538 0.6534 0.9227 0.7608 0.8356 -0.4874
    Achc 0.6024 0.4358 -0.6313 -0.4231 -0.6057 -0.4870 1.0000 -0.5157 0.0623 -0.3139 -0.3024 -0.3900 0.6086
    ACIW -0.6606 -0.4539 0.8973 0.5657 0.8677 0.7538 -0.5157 1.0000 0.2894 0.7745 0.8688 0.8297 -0.2516
    ACM 0.0566 0.4234 0.2932 0.7552 0.1999 0.6534 0.0623 0.2894 1.0000 0.6292 0.3171 0.4927 -0.1822
    ACN -0.5522 -0.0658 0.8414 0.7104 0.7411 0.9227 -0.3139 0.7745 0.6292 1.0000 0.8545 0.8457 -0.3193
    ADBE -0.6880 -0.3704 0.8717 0.4519 0.8115 0.7608 -0.3024 0.8688 0.3171 0.8545 1.0000 0.8170 -0.1243
    ADI -0.6088 -0.2713 0.8102 0.6118 0.8170 0.8356 -0.3900 0.8297 0.4927 0.8457 0.8170 1.0000 -0.4116
    ADM 0.4409 0.1779 -0.4257 -0.4653 -0.4128 -0.4874 0.6086 -0.2516 -0.1822 -0.3193 -0.1243 -0.4116 1.0000
    
  • Answer # 2

    If the essence of the question is to change this code so that the data is read from CSV files from one directory, then this can be done like this:

    Import Pandas AS PD
    From Pathlib Import Path
    P= Path (R "D: \ Download \ .data")
    DF= (PD
          .concat (
            [Pd.Read_CSV (F, UseCols= ["Date", "Close"]). Assign (Symbol= F.STEM)
             for f in p.glob ("*. CSV")]
            Ignore_Index= True)
          .pivot (index= "Date", columns= "symbol", values ​​= "close"))
    Corr_DF= df.corr (Method= 'Pearson')
    Pd.Options.display.float_format= '{:,. 4f}'. Format
    Print (Corr_DF)
    

    Result:

    Symbol ABBV ABC ABT ACAD ACG ACGL ACHC ACIW ACM ACN ADBE ADI ADM
    Symbol.
    ABBV 1.0000 0.5517 -0.8029 -0.2716 -0.8211 -0.6080 0.6024 -0.6606 0.0566 -0.5522 -0.6880 -0.6088 0.4409
    ABC 0.5517 1.0000 -0.3774 0.0644 -0.4869 -0.1198 0.4358 -0.4539 0.4234 -0.0658 -0.3704 -0.2713 0.1779
    ABT -0.8029 -0.3774 1.0000 0.6249 0.8910 0.8594 -0.6313 0.8973 0.2932 0.8414 0.8717 0.8102 -0.4257
    ACAD -0.2716 0.0644 0.6249 1.0000 0.5182 0.8295 -0.4231 0.5657 0.7552 0.7104 0.4519 0.6118 -0.4653
    ACC -0.8211 -0.4869 0.8910 0.5182 1.0000 0.7766 -0.6057 0.8677 0.1999 0.7411 0.8115 0.8170 -0.4128
    ACGL -0.6080 -0.1198 0.8594 0.8295 0.7766 1.0000 -0.4870 0.7538 0.6534 0.9227 0.7608 0.8356 -0.4874
    Achc 0.6024 0.4358 -0.6313 -0.4231 -0.6057 -0.4870 1.0000 -0.5157 0.0623 -0.3139 -0.3024 -0.3900 0.6086
    ACIW -0.6606 -0.4539 0.8973 0.5657 0.8677 0.7538 -0.5157 1.0000 0.2894 0.7745 0.8688 0.8297 -0.2516
    ACM 0.0566 0.4234 0.2932 0.7552 0.1999 0.6534 0.0623 0.2894 1.0000 0.6292 0.3171 0.4927 -0.1822
    ACN -0.5522 -0.0658 0.8414 0.7104 0.7411 0.9227 -0.3139 0.7745 0.6292 1.0000 0.8545 0.8457 -0.3193
    ADBE -0.6880 -0.3704 0.8717 0.4519 0.8115 0.7608 -0.3024 0.8688 0.3171 0.8545 1.0000 0.8170 -0.1243
    ADI -0.6088 -0.2713 0.8102 0.6118 0.8170 0.8356 -0.3900 0.8297 0.4927 0.8457 0.8170 1.0000 -0.4116
    ADM 0.4409 0.1779 -0.4257 -0.4653 -0.4128 -0.4874 0.6086 -0.2516 -0.1822 -0.3193 -0.1243 -0.4116 1.0000