-1

I have two data frames with different shapes, fx_data

macronews

I'm trying the join them by matching the index column. I tried

fx_news_concantate = fx_data.join(macronews, sort=False)

But it keeps sorting the date time incorrectly, starting from 01/01/2018

fx_news_concantate

Is there any way around this? .merge keeps returning me error.

Community
  • 1
  • 1

2 Answers2

1

Seems like you're looking to use pd.concat instead of pd.join (seeing as you even called your variable fx_news_concatenate!).

Try:

fx_news_concatenate = pd.concat([macronews,fx_date],axis=1,join='inner')

The "axis=1" indicates you want to concatenate column-wise (not row-wise) and "join=inner" specifies how you want to handle the axis indices.

Hope it helps :)

For more details, you can refer the Pandas documentation here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html

Here's a good answer to the difference between merge, join and concat operations: Difference(s) between merge() and concat() in pandas

Madoo
  • 115
  • 6
  • I think concat() only works if the index columns of both data frames are exactly equal, which gives me an error. Sorry if it's not clear in the question. – Kingvader Wong Jun 25 '18 at 19:40
  • No problem. Concat will work even if index columns are not exactly equal. It depends on how you choose to deal with the indices. Are your indices unique, i.e. not duplicated within a dataframe? If so, simply try: pd.concat([macronews,fx_date],axis=1). – Madoo Jun 25 '18 at 19:46
  • Thank you! The duplicated indices were the ones causing the problem! – Kingvader Wong Jun 25 '18 at 21:57
  • Happy to help :) – Madoo Jun 25 '18 at 22:04
0

Try using concat instead

fx_news_concanate = pd.concat([macronews, fx_data], axis=1, join='inner')

Documentation is here

Inder
  • 3,350
  • 8
  • 22
  • 35
  • Sorry, they don't have the same shape. – Kingvader Wong Jun 25 '18 at 19:26
  • in that case may i suggest you split the timestamp, the issue can very well be from the fact that the timestamp for both is different. i.e. the dates are same but the time is different – Inder Jun 25 '18 at 19:36