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,
JavaScript
x
13
13
1
>>> mydf
2
col_A col_B
3
0 1 4
4
1 2 5
5
2 3 6
6
>>> colname = 'col1'
7
>>> locals()[f'df_{colname}'] = mydf.col_A
8
>>> df_col1
9
0 1
10
1 2
11
2 3
12
Name: col_A, dtype: int64
13