1

Let's say I have a dataframe in python like below:

DateTime   A, B
12/03/2020 1, 40
12/03/2020 2, 30
13/03/2020 12, 32
13/03/2020 90, 2

I want to split these into two dataframes each with unique dates like below

df1

DateTime,  A, B
12/03/2020 1, 40
13/03/2020 12, 32

df2

DateTime,  A, B
12/03/2020 2, 30
13/03/2020 90, 2

The order is important. I.e. The first date of each couple should all be in one dataframe and the second date in another.

How can one achieve this in pandas?

NB: this answer, chosen as duplicate does not help me. I am trying to achieve something different.

bcsta
  • 1,963
  • 3
  • 22
  • 61
  • try `{f'df{key}' : data for key, data in df.groupby(df.groupby('DateTime').cumcount())}` – Umar.H Jul 16 '20 at 11:30
  • I don't know why this question has been closed. The link to the answer does not help me whatsoever. People should read the question carefully before they close someone's post. – bcsta Jul 16 '20 at 11:32
  • @Manakin this does help me. I don't know If you can post it as an answer now that the question has been marked as duplicate. – bcsta Jul 16 '20 at 11:37
  • It's fine - I can find another duplicate this Q has been asked before :) don't worry about the dupe hammer, I think 60-70 % of questions are duplicates – Umar.H Jul 16 '20 at 11:38
  • I wouldn't have asked the question if SEO did a better job in recognizing it. – bcsta Jul 16 '20 at 11:39
  • 1
    There's no cumcount needed though. Updated the dupe to generate a df of lists, which is more suited here @Manakin – yatu Jul 16 '20 at 11:41
  • @Manakin another issue is that this return a dict rather than two dataframes – bcsta Jul 16 '20 at 11:48
  • are you looking for something like [this](https://stackoverflow.com/questions/44821090/split-dataframe-randomly-dependent-on-unique-values)? – FObersteiner Jul 16 '20 at 12:21

0 Answers0