0

I have a list of locations

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

and a pandas data frame "DF"

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

I want to create 3 different data frames for HOME, Office, SHOPPING using for loop, but I am not able to do it.

I am new to python

Please help.

Thanks lucy

4
  • What's your question? You don't know how to write a for loop? Commented Feb 13, 2016 at 1:47
  • I know how to write for loop. My question is how to create 3 different data frames using for loop like df1=DF[DF.Start_Location==locations[0]]. Hope this helps Commented Feb 13, 2016 at 1:49
  • Not really. Your code looks OK to me. Commented Feb 13, 2016 at 1:58
  • make your for loop a variable on the locations so that you do locations[i], and store them in a list of dataframe dfs[i] = Commented Feb 13, 2016 at 3:04

3 Answers 3

5

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,

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

thanks for the solution, but I want to create these dfs without using read_csv, because the main dataframe is already available, and also if the list of location is more, lets say 20 then giving there name in left side of equal to sign will be little bit tidy. Is there any other way to do this?
read_csv() is only for demo, you don't need to call it. I edited the answer to use globals() .
0

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.