Create multiple dataframe using for loop in python 2.7

后端 未结 3 657
余生分开走
余生分开走 2021-01-26 01:00

I have a list of locations

[\"HOME\", \"Office\", \"SHOPPING\"]

and a pandas data frame \"DF\"

Start_Location  End_Location            


        
相关标签:
3条回答
  • 2021-01-26 01:28

    Use groupby() and then call it's get_group() method:

    import pandas as pd
    import io
    
    text = b"""Start_Location  End_Location    Date
    OFFICE          HOME            3-Apr-15
    OFFICE          HOME            3-Apr-15
    HOME            SHOPPING    3-Apr-15
    HOME            SHOPPING    4-Apr-15
    HOME            SHOPPING    4-Apr-15
    SHOPPING    HOME            5-Apr-15
    SHOPPING    HOME            5-Apr-15
    HOME            SHOPPING    5-Apr-15"""
    
    locations = ["HOME", "OFFICE", "SHOPPING"]
    
    df = pd.read_csv(io.BytesIO(text), delim_whitespace=True)
    g = df.groupby("Start_Location")
    for name, df2 in g:
        globals()["df_" + name.lower()] = df2
    

    but I think add global variables in a for loop isn't a good method, you can convert the groupby to a dict by:

    d = dict(iter(g))
    

    then you can use d["HOME"] to get the data.

    0 讨论(0)
  • 2021-01-26 01:36

    You could probably have a dictionary and you want to convert it to some dataframes, based on the keys of your dictionary:

    gbl = globals()
    for keys, values in dictionary.items():
       gbl['df_min'+ str(keys)] = pd.DataFrame(values)
    
    0 讨论(0)
  • 2021-01-26 01:41

    I got the answer which I was looking for

    import pandas as pd
    gbl = globals()
    for i in locations:
    gbl['df_'+i] = df[df.Start_Location==i]
    

    This will create 3 data frames df_HOME, df_office and df_SHOPPING

    Thanks,

    0 讨论(0)
提交回复
热议问题