1

I am learning python and pandas and am having trouble overcoming an error while trying to subset a data frame.

I have an input data frame:

df0-
    Index   Group   Value
    1       A       10
    2       A       15
    3       B       20
    4       C       10
    5       C       10

    df0.dtypes-
    Group   object
    Value   float64

That I am trying to split out into unique values based off of the Group column. With the output looking something like this:

df1-
Index   Group   Value
1       A       10
2       A       15

df2-
Index   Group   Value
3       B       20

df3-
Index   Group   Value
4       C       10
5       C       10

So far I have written this code to subset the input:

UniqueGroups = df0['Group'].unique().tolist()
OutputFrame = {}
for x in UniqueAgencies:          
    ReturnFrame[str('ConsolidateReport_')+x] = UniqueAgencies[df0['Group']==x]

The code above returns the following error, which I can`t quite work my head around. Can anyone point me in the right direction?

*** TypeError: list indices must be integers or slices, not str
0

1 Answer 1

1

you can use groupby to group the column

for _, g in df0.groupby('Group'):
  print g
Sign up to request clarification or add additional context in comments.

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.