Dhruv Kadia
Dhruv Kadia

Reputation: 91

Split DataFrame by grouping column values in pandas

I have a DataFrame

S   C
3   2
3   2
3   2
3   2
3   2
3   2
1   4
1   4
1   4
1   4
1   4
1   4

How can i split the dataframe so that one dataframe has 3 and 2 in S and C and other has 1 and 4 in S and C

Upvotes: 2

Views: 5609

Answers (1)

piRSquared
piRSquared

Reputation: 294258

Use a comprehension with groupby

  1. The key element is df.groupby which provides the grouping you desire.
  2. However, you need to facilitate this into a list so that you have "split" it into separate data frames.
  3. You can iterate through the groupby object which passes a tuple where the first element is the name of the group (we mask this with _) and the second element is the individual data frame.
  4. By using a comprehension, we can iterate through the groupby and capture the second element of each tuple... thus creating a list of data frames.

See:
List Comprehensions
Grouping Stuff


list_of_df = [g for _, g in df.groupby(['NUMBER_OF_TRIPS', 'SERVICE_CLASS'])]

print(*list_of_df, sep='\n\n')

    NUMBER_OF_TRIPS  SERVICE_CLASS
6                 1              4
7                 1              4
8                 1              4
9                 1              4
10                1              4
11                1              4

   NUMBER_OF_TRIPS  SERVICE_CLASS
0                3              2
1                3              2
2                3              2
3                3              2
4                3              2
5                3              2

Upvotes: 7

Related Questions