-1

I'm working with a few DataFrames. One is pulling from a newer csv, with added columns, and continuing dates. However, most columns are the same. I would like to combine them, to get one DataFrame with all dates and all columns present?

the csvs are something like these, with some same columns and some different columns, but with all unique dates:

df1:
day     alice  bob
8/11    0      0
8/25    2      5
9/1     2      0

df2:
day     alice  charlie
9/12    1      1
9/25    2      3
9/1     2      1

resulting dataFrame should be something like this, with one column of dates and all columns present (i can convert NaN to fillna later, i just need to know how to combine):


df3: 
day     alice  bob  charlie
8/11    0      0      0 
8/25    2      5      0 
9/1     2      0      0
9/12    1      0      1
9/25    2      0      3
9/1     2      0      1

When I combine my actual 2 DataFrames with pd.concat, i get the following error:

AssertionError: Number of manager items must equal union of block items
# manager items: 65, # tot_items: 66

Not sure what the issue could be. In the meantime, thanks and you're awesome : )

neutrino
  • 894
  • 8
  • 23

1 Answers1

1

You can use concat:

pd.concat((df1,df2), sort=False).fillna(0)

    day  alice  bob  charlie
0  8/11      0  0.0      0.0
1  8/25      2  5.0      0.0
2   9/1      2  0.0      0.0
0  9/12      1  0.0      1.0
1  9/25      2  0.0      3.0
2   9/1      2  0.0      1.0
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • on my actual dataframe, im getting AssertionError: Number of manager items must equal union of block items # manager items: 65, # tot_items: 66 – neutrino May 06 '20 at 20:51
  • That must happen somewhere else in the code, or your data looks differently from the sample data :-). – Quang Hoang May 06 '20 at 20:53
  • it is different from the sample data (its sensitive). But the structure is the same. some of the same columns, some new columns, and continuing dates. – neutrino May 06 '20 at 20:54
  • See [this answer](https://stackoverflow.com/a/39616022/4238408) specifically the second point. – Quang Hoang May 06 '20 at 20:56