cloemdz
cloemdz

Reputation: 3

How to access a pandas dataframe by string?

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!

Upvotes: 0

Views: 819

Answers (2)

dspencer
dspencer

Reputation: 4471

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.

Upvotes: 1

caue guedes
caue guedes

Reputation: 31

You can use eval on python to transform string into code

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

Upvotes: 2

Related Questions