Issue
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
Solution
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
Answered By - Ammar Sabir Cheema
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.