Returning a dataframe in python function

前端 未结 4 1934
-上瘾入骨i
-上瘾入骨i 2021-02-05 21:03

I am trying to create and return a data frame from a python function

def create_df():
    data = {\'state\': [\'Ohio\',\'Ohio\',\'Ohio\',\'Nevada\',\'Nevada\'],         


        
4条回答
  •  遥遥无期
    2021-02-05 21:09

    I'm kind of late here, but what about creating a global variable within the function? It should save a step for you.

    def create_df():
    
        global df
    
        data = {
        'state': ['Ohio','Ohio','Ohio','Nevada','Nevada'],
        'year': [2000,2001,2002,2001,2002],
        'pop': [1.5,1.7,3.6,2.4,2.9]
        }
    
        df = pd.DataFrame(data)
    

    Then when you run create_df(), you'll be able to just use df.

    Of course, be careful in your naming strategy if you have a large program so that the value of df doesn't change as various functions execute.

    EDIT: I noticed I got some points for this. Here's another (probably worse) way to do this using exec. This also allows for multiple dataframes to be created, if desired.

    import pandas as pd
    
    def create_df():
        data = {'state': ['Ohio','Ohio','Ohio','Nevada','Nevada'],
               'year': [2000,2001,2002,2001,2002],
               'pop': [1.5,1.7,3.6,2.4,2.9]}
        df = pd.DataFrame(data)
        return df
    
    ### We'll create three dataframes for an example
    for i in range(3):
        exec(f'df_{i} = create_df()')
    

    Then, you can test them out:

    Input: df_0

    Output:

        state  year  pop
    0    Ohio  2000  1.5
    1    Ohio  2001  1.7
    2    Ohio  2002  3.6
    3  Nevada  2001  2.4
    4  Nevada  2002  2.9
    

    Input: df_1

    Output:

        state  year  pop
    0    Ohio  2000  1.5
    1    Ohio  2001  1.7
    2    Ohio  2002  3.6
    3  Nevada  2001  2.4
    4  Nevada  2002  2.9
    

    Etc.

提交回复
热议问题