0

Apologies if this is a simple question, but let's assume I have multiple pandas data frames that look like this:

name    date          value
'a'     '2018-07-28'   0
'a'     '2018-07-29'   1

And another that looks like this:

name    date          value
'b'     '2018-07-28'   2
'b'     '2018-07-29'   3

And essentially what I want to do is create a GroupBy or another dataframe that contains

date          name    value
'2018-07-28'  'a'     0
'2018-07-28'  'b'     2
'2018-07-29'  'a'     1
'2018-07-29'  'b'     3

There has to be a simple way to do this, but I am not sure.

The two original dataframes I get by iterating through a method which returns each dataframe one by one.

Thanks!

SwiftySwift
  • 477
  • 4
  • 13
  • Looks like https://stackoverflow.com/questions/28669482/appending-pandas-dataframes-generated-in-a-for-loop works quite well. – SwiftySwift Jul 28 '18 at 18:19
  • Just to clarify, you want to create a new dataframe with both of the previous dataframes and then sort it by a specific attribute? If so you can do something like this: new_df = df1.append(df2).sort_values('date') If you have more than 2 I recommend pd.concat. – SamrajM Jul 28 '18 at 18:21

1 Answers1

1

Using pd.concat

Ex:

import pandas as pd

df1 = pd.DataFrame({"name": ['a', 'a'], "date":['2018-07-28', '2018-07-29'], "value": [0, 1]})
df2 = pd.DataFrame({"name": ['b', 'b'], "date":['2018-07-28', '2018-07-29'], "value": [2, 3]})
print(pd.concat([df1, df2]).sort_values(by=['date']))

Output:

         date name  value
0  2018-07-28    a      0
0  2018-07-28    b      2
1  2018-07-29    a      1
1  2018-07-29    b      3
Rakesh
  • 81,458
  • 17
  • 76
  • 113