1

I have a Python data frame that I want to subdivide by row BUT in 32 different slices (think of a large data set chopped by row into 32 smaller data sets). I can manually divide the data frames in this way:

df_a = df[df['Type']=='BROKEN PELVIS']

df_b = df[df['Type']=='ABDOMINAL STRAIN']

I'm assuming there is a much more Pythonic expression someone might like to share. I'm looking for something along the lines of:

for i in new1:
    df_%s= df[df['#RIC']=='%s'] , %i

Hope that makes sense.

1 Answer 1

5

In these kind of situations I think it's more pythonic to store the DataFrames in a python dictionary:

injuries = {injury: df[df['Type'] == injury] for injury in df['Type'].unique()}

injuries['BROKEN PELVIS']  # is the same as df_a above

Most of the time you don't need to create a new DataFrame but can use a groupby (it depends what you're doing next), see http://pandas.pydata.org/pandas-docs/stable/groupby.html:

g = df.groupby('Type')

Update: in fact there is a method get_group to access these:

In [21]: df = pd.DataFrame([['A', 2], ['A', 4], ['B', 6]])

In [22]: g = df.groupby(0)

In [23]: g.get_group('A')
Out[23]:
   0  1
0  A  2
1  A  4

Note: most of the time you don't need to do this, apply, aggregate and transform are your friends!

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

1 Comment

thanks for the input really appreciate it. i want to write something funny to let you know that i'm extremely grateful but, alas, my sense of humor is a little dark and i don't want to offend the people of stack overflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.