0

I want to sort/groupby/concatenate data in a list by date, but without changing the order. I have the following list stored in

loop_1 = []

and the sample data in loop_1 is the following:

enter image description here

I want to achieve this:

enter image description here

I've tried:

df = pd.concat(loop_1,axis=1)

also:

df = pd.concat(loop_1)
df.reset_index(inplace=True)
df = df.groupby(['date']).first()

also:

df = pd.concat(loop_1)
df.reset_index(inplace=True)
df = df.groupby(['date'] sort=False).first()

but still cannot achieve the desired result.

kobo
  • 35
  • 6
  • I think you need to `merge` `outer` those 2 dataframe. https://stackoverflow.com/questions/53645882/pandas-merging-101 – garagnoth Oct 03 '21 at 14:37
  • Your source data is in a list, you say ? How does it look like ? Your like shown is empty loop_1 = [] – EBDS Oct 03 '21 at 14:41
  • It is stored in loop_1 -> loop_1.append(data). – kobo Oct 03 '21 at 14:44
  • Please show sample data in list `loop_1`. – SeaBean Oct 03 '21 at 16:13
  • I edited the post which shows the sample data in list loop_1. – kobo Oct 03 '21 at 16:19
  • Your date format is difficult to sort them in sequence. What's the year you assume for dates in December and what's year for dates in January ? – SeaBean Oct 03 '21 at 16:51
  • In the original code 15 different loops occur and every loop is for different period (first is Dec 2006-Jan 2007, second is Dec 2007-Jan 2008, third is Dec 2008-Jan 2009 and so long). After every loop I sort the result in loop_1. After the final loop is completed I want to sort the values in loop_1 by Month and Day but also to keep the Dec-Jan order (as shown in the desired outcome). Date format is like that because I want to sort/group the values by month and day. – kobo Oct 03 '21 at 17:23

1 Answers1

0

You can get this result by using the pandas merge function.

pd.merge(df1, df2, on='date', how='outer')
Brandon
  • 21
  • 2
  • Thank you for the suggestion. The problem is that I have a list stored in loop_1 and not separate dataframes. – kobo Oct 03 '21 at 16:02
  • @kobo I'd suggest making it a Pandas DataFrame to make it easier to work with. If you show what loop_1 looks like, I might be able to help. It looks like they're 2 columns, but you say it's 1. How are they separated? – Brandon Oct 03 '21 at 16:45
  • I edited the post which shows the sample data in list loop_1. In the original code 15 different loops occur and every loop is for different period (first is Dec 2006-Jan 2007, second is Dec 2007-Jan 2008, third is Dec 2008-Jan 2009 and so long). After every loop I sort the result in loop_1. After the final loop is completed I want to sort the values in loop_1 by Month and Day but also to keep the Dec-Jan order (as shown in the desired outcome). – kobo Oct 03 '21 at 17:18