0

I have a data which looks like below

data = [(u'Del', datetime.datetime(2019, 11, 1, 0, 0), 59L), (u'Bom', datetime.datetime(2019, 11, 1, 0, 0), 449L), (u'Del', datetime.datetime(2019, 12, 1, 0, 0), 0L), (u'Bom', datetime.datetime(2019, 12, 1, 0, 0), 45L)]

Now I want to sub group the data based on time such that it looks something like this

data = [
         [(u'Del', datetime.datetime(2019, 11, 1, 0, 0), 59L), (u'Bom', datetime.datetime(2019, 11, 1, 0, 0), 449L)] 
        ,[(u'Del', datetime.datetime(2019, 12, 1, 0, 0), 0L), (u'Bom', datetime.datetime(2019, 12, 1, 0, 0), 45L)]
       ]

As you can see, now it is a list of lists where there are two lists inside a list where each list contains similar datetime. For example the first sublist looks like this

[(u'Del', datetime.datetime(2019, 11, 1, 0, 0), 59L), (u'Bom', datetime.datetime(2019, 11, 1, 0, 0), 449L)]

Here the items of the first sublist contains similar date time which is datetime.datetime(2019, 11, 1, 0, 0)

The second sublist looks like this

[(u'Del', datetime.datetime(2019, 12, 1, 0, 0), 0L), (u'Bom', datetime.datetime(2019, 12, 1, 0, 0), 45L)]

Here the items of the first sublist contains similar date time which is datetime.datetime(2019, 12, 1, 0, 0)

I can sort the data based on datetime by doing something like this (though data is already sorted by datetime in this case)

import pandas as pd
import datetime
import psycopg2

df = pd.DataFrame(data)
df['Date'] =pd.to_datetime(df[1])
df = df.sort_values(by='Date')

But I can't group them based on the sorted time. How do I achieve this using pandas?

Souvik Ray
  • 2,899
  • 5
  • 38
  • 70
  • There are syntax errors in your example data, please edit your post. – eva-vw Jan 03 '20 at 20:00
  • @eva-vw The data is correct. You have to use imports that I have used to wrangle the data. – Souvik Ray Jan 03 '20 at 20:01
  • Please have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide a sample of the actually dataframe you're trying to group. A [mcve] should include all of the code necessary to reproduce the issue (and _only_ the necessary code), as well as a sample input and output. – G. Anderson Jan 03 '20 at 20:10
  • @G.Anderson I have now fixed the data example. – Souvik Ray Jan 03 '20 at 20:29

1 Answers1

1

You can do the following

df = pd.DataFrame(data)
df.columns = ['place','date','value']


output = [x[1].values for x in df.groupby(date)]

output looks like:

[[[u'Del', Timestamp('2019-11-01 00:00:00'), 59], [u'Bom', Timestamp('2019-11-01 00:00:00'), 449]], [[u'Del', Timestamp('2019-12-01 00:00:00'), 0], [u'Bom', Timestamp('2019-12-01 00:00:00'), 45]]]
Souvik Ray
  • 2,899
  • 5
  • 38
  • 70
Roshan Santhosh
  • 677
  • 3
  • 9