1
from functools import reduce    
for j in gpw_ticker:
    try:
        zadl = pd.read_csv(r'C:\Users\AdamPer\Desktop\Python\Valueinvesting\zadluzenie\zadluzenie_{}.csv'.format(j))
        war_ryn = pd.read_csv(r'C:\Users\AdamPer\Desktop\Python\Valueinvesting\wart_rynkowa\wart_rynkowa_{}.csv'.format(j))
        rento = pd.read_csv(r'C:\Users\AdamPer\Desktop\Python\Valueinvesting\rentownosc\rentownosc_{}.csv'.format(j))
        plynn = pd.read_csv(r'C:\Users\AdamPer\Desktop\Python\Valueinvesting\plynnosc\plynnosc_{}.csv'.format(j))
        dfs = [zadl, war_ryn, rento, plynn]
        df = reduce(lambda left, right: pd.merge(left, right, on = 'rows', how = 'outer'), dfs)
        df.to_csv(r'C:\Users\AdamPer\Desktop\Python\Valueinvesting\baza\{}.csv'.format(j), index = False)
    except Exception as e:
        print(j, str(e))

It works quite well but the problem occurs when file does not exist:

code output

I want to skip non-existing files and merge the other. So for ALR I want to merge zadl, war_ryn and rento and skip plynn because it doesn't exist.

Should I first check if each file exists and then merge?

colidyre
  • 4,170
  • 12
  • 37
  • 53

0 Answers0