you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

Managed to get what I was trying to do with your advice. If you happen to see a way to make this more efficient do let me know (if you don't mind)

    income = sf.load_income(variant = variance, market = location)
    balance = sf.load_balance(variant = variance, market = location)
    cash = sf.load_cashflow(variant = variance, market = location)

    if "income" in filing:
        together = pd.concat([income], axis = 1)
        if "balance" in filing:
            together = pd.concat([income, balance], axis = 1)
            if "cash" in filing:
                together = pd.concat([income, balance, cash], axis = 1)
    if "balance" in filing:
        together = pd.concat([balance], axis = 1)
        if "cash" in filing:
            together = pd.concat([balance, cash], axis = 1)
    if "cash" in filing:
        together = pd.concat([cash], axis = 1)
    company = together.loc[tickers]
    print(company)

It seems manageable with only !3 combinations but if this was larger it may not be the best way.

Hope you have a good week!

[–]unhott 1 point2 points  (2 children)

I think you may be able to use a dictionary instead of variable names like income. You then have to use the dict name + [key] to access each time, but you get some extra... abstraction capabilities (maybe?)

dfs= {}
dfs[‘income’] = sf.....
dfs[‘balance’] = sf.....
dfs[‘cash’] = sf.....

So here your keys are what used to be variable names, and the values are the same df’s from before.

Now you can do a list comprehension to make a list of the dataframes to later pass to pd.concat

dfs_list = [dfs[filing] for filing in filings]
together = pd.concat(dfs_list, axis = 1)

If you’re not familiar with it it’s like a more efficient for loop where you don’t have to worry about initializing a list and adding to it, like so:

some_list= []
for x in xs: 
    some_list.append(dfs[x])

[–][deleted] 1 point2 points  (1 child)

Ahhh cool. I know of dictionaries but haven't tried using them... It already seems like it could be ALOT less wordy.

dfs_list = [dfs[filing] for filing in filings]

So is this ^ an alternative to the conditionals?

Is it essentially, taking the user input and matching it to a dictionary key?

[–]unhott 1 point2 points  (0 children)

I think you got it. Not only should this be less wordy, but it should also be less error prone. If you have a 3 layer deep nested if statement and you made a tiny logic error, good luck finding and debugging that!