In [182]: colname
Out[182]: ‘col1’
In [183]: x= ‘df_’ + colname
In [184]: x
Out[184]: ‘df_col1’
May I know how to create a new pandas data frame with x
, such that the new data frame’s name would be df_col1
Advertisement
Answer
You can use the locals() function as given below,
>>> mydf col_A col_B 0 1 4 1 2 5 2 3 6 >>> colname = 'col1' >>> locals()[f'df_{colname}'] = mydf.col_A >>> df_col1 0 1 1 2 2 3 Name: col_A, dtype: int64