2

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

0

1 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your code, since i am new to python, can you please help me explaining how this code works?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.