How do you give a dataframe name dynamically in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a string input, our task is to write a Python program to create a variable from that input [as a variable name] and assign it to some value. Below are the methods to create dynamically named variables from user input.

    Using globals[] method to create dynamically named variables

    Here we are using the globals[] method for creating a dynamically named variable and later assigning it some value, then finally printing its value.

    Python3

    Dynamic_Variable_Name = "geek"

    globals[][Dynamic_Variable_Name] = 2020

    print[geek]

    Output:

    2020

    Using locals[] method to create dynamically named variables

    Here we are using the locals[] method for creating dynamically named variable and later assigning it some value, then finally printing its value.

    Python3

    Dynamic_Variable_Name = "geek"

    locals[][Dynamic_Variable_Name] = 2020

    print[geek]

    Output:

    2020

    Using exec[]method to create dynamically named variables

    Here we are using the exec[] method for creating dynamically named variable and later assigning it some value, then finally printing its value.

    Python3

    Dynamic_Variable_Name = "geek"

    exec["%s = %d" % [Dynamic_Variable_Name, 2020]]

    print[geek]

    Output:

    2020

    Using vars[]method to create dynamically named variables

    Here we are using the vars[] method for creating dynamically named variable and later assigning it some value, then finally printing its value.

    Python3

    Dynamic_Variable_Name = "geek"

    vars[][Dynamic_Variable_Name] = 2020

    print[geek]

    Output:

    2020

    Datasnips was built out of frustration in wasting time searching for Data Science & AI code snippets through old projects, documentation and internet sources.

    We wanted a platform to easily find & organise our snippets, to share them with the community and discover those shared by other users.

    At Datasnips you can quickly search, discover and bookmark snippets from the community, curate your own snippet library for easy reference and share your own snippets for others to use.

    Our aim is for Datasnips to be the place where everyone can find the code snippet they need. Sign up to be part of the community.

    • Code Snippets
    • Blog
    • Login

    Python

    Data Preprocessing

    In this example both the values in the columns and the column names are created dynamically.

    Here we dynamically create three columns that multiply the column A in our dataframe by each of the numbers in the list "multipliers".

    multipliers = [2,3,4]
    for i in multipliers:
        df[f'times_{i}'] = df['A'] * i
    
    df.columns
    
    >> Index[['A', 'B', 'times_2', 'times_3', 'times_4'], dtype='object']

    By detro - Last Updated March 29, 2022, 7:34 p.m.

    Did you find this snippet useful?

    Sign up to bookmark this in your snippet library

    COMMENTS

    RELATED SNIPPETS

    Find Snippets by Language

    Find Snippets by Use

    Question :

    Create new dataframe in pandas with dynamic names also add new column

    I have a dataframe df

     df = pd.DataFrame[{'A':['-a',1,'a'], 
                   'B':['a',np.nan,'c'],
                   'ID':[1,2,2],
                    't':[pd.tslib.Timestamp.now[],pd.tslib.Timestamp.now[],
                        np.nan]}]
    

    Added a new column

    df['YearMonth'] = df['t'].map[lambda x: 100*x.year + x.month]
    

    Now I want to write a function or macro which will do date comparasion, create a new dataframe also add a new column to dataframe.

    I tried like this but seems I am going wrong:

    def test[df,ym]:
        df_new=df
        if[ym 

    Bài mới nhất

    Chủ Đề