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?

4
  • There are syntax errors in your example data, please edit your post. Commented Jan 3, 2020 at 20:00
  • @eva-vw The data is correct. You have to use imports that I have used to wrangle the data. Commented Jan 3, 2020 at 20:01
  • Please have a look at How to make good pandas examples and provide a sample of the actually dataframe you're trying to group. A minimal reproducible example should include all of the code necessary to reproduce the issue (and only the necessary code), as well as a sample input and output. Commented Jan 3, 2020 at 20:10
  • @G.Anderson I have now fixed the data example. Commented Jan 3, 2020 at 20:29

1 Answer 1

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

1 Comment

just exactly what I was looking for. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.