Skip to content Skip to sidebar Skip to footer

Create List Of Several Datasets

I have several datasets like df_1,df_2,...df_100. First I want to create a list of these datasets. df=[df_1,df_2,...,df_100] This is what I did which did not work for me. df=[] fo

Solution 1:

Use built-in functions globals or locals to get variable by name

>>>[globals()[d] for d in df]

Example:

>>>df_1
                   A         B         C
9l6rvsotz5  0.209350 -1.360556  0.059560
jTonmSOIVv  1.046584  0.251718  0.567056
eGaK0n8y9N -0.347716 -0.292623  0.591843

>>>df_2
                   A         B         C
TIVsJWSDWe -0.169969  0.345766  0.674683
EJjXuhL3pi -0.527015 -1.089954 -1.658116
dm3IYAyC7z  1.653666 -0.203685 -1.441150

>>>df_3
                   A         B         C
DbmE1sc3MI  0.215871 -0.382257  0.662477
9qZd6bvPVy  0.150985  0.135556  0.308615
qiVrxD64IF -1.384027  0.765303 -0.734394

>>>df = ["df_{}".format(i) for i inrange(1, 4)]>>>df
['df_1', 'df_2', 'df_3']

>>>pd.concat([globals()[d] for d in df], ignore_index=True)
          A         B         C
0  0.209350 -1.360556  0.059560
1  1.046584  0.251718  0.567056
2 -0.347716 -0.292623  0.591843
3 -0.169969  0.345766  0.674683
4 -0.527015 -1.089954 -1.658116
5  1.653666 -0.203685 -1.441150
6  0.215871 -0.382257  0.662477
7  0.150985  0.135556  0.308615
8 -1.384027  0.765303 -0.734394

Post a Comment for "Create List Of Several Datasets"