0

I have a lot of dataframe that have similar names for example:

NB_2014= pd.read_csv("E:/NB_2014.CSV", sep=';', thousands='.', decimal=',')
NB_2015= pd.read_csv("E:/NB_2015.CSV", sep=';', thousands='.', decimal=',')
NB_2016= pd.read_csv("E:/NB_2016.CSV", sep=';', thousands='.', decimal=',')
NB_2017= pd.read_csv("E:/NB_2017.CSV", sep=';', thousands='.', decimal=',')
NB_2018= pd.read_csv("E:/NB_2018.CSV", sep=';', thousands='.', decimal=',')

and so on. I was wandering if there is a way to call the right dataframe just by having the year that I need, for example I tried:

year_needed = 2018
print("NB_"+str(year_needed)["col_needed"])

but I get the following error:

TypeError: string indices must be integers

Is there a way to access the dataframe just by having a piece of its name?

Thanks in advance!

2 Answers 2

2

The best way to achieve this would be to store your DataFrames in a dictionary, e.g.:

dfs = {
    2014: pd.read_csv("E:/NB_2014.CSV", sep=';', thousands='.', decimal=',')
    2015: pd.read_csv("E:/NB_2015.CSV", sep=';', thousands='.', decimal=',')
    2016: pd.read_csv("E:/NB_2016.CSV", sep=';', thousands='.', decimal=',')
    2017: pd.read_csv("E:/NB_2017.CSV", sep=';', thousands='.', decimal=',')
    2018: pd.read_csv("E:/NB_2018.CSV", sep=';', thousands='.', decimal=',')
}

Then access, for example, using:

dfs[2018]

When you have a list of items like this which you would like to access by name, a dictionary-based approach is much preferred over eval, which is a dangerous coding practice and should not be considered when you know in advance how you will access the relevant DataFrame.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use eval on python to transform string into code

print(eval('NB_' + str(year_needed) + '["col_needed"]' ))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.