0

I have a pandas DataFrame of a datetime series and a pandas DataFrame with a numbered index:

​How do I set the index of the DataFrame with the numbered index to match the index of the DataFrame with the datetime series?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
lopezdp
  • 1,538
  • 3
  • 21
  • 38

2 Answers2

1

If same length of both DataFrames:

df1.index = df2.index

Or:

df1= df1.set_index(df2.index)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • thanks man I knew it was going to be something ridiculously easy. I knew I was going to get downvoted for this one just go tired of looking in docs. Thanks for the quick answer! – lopezdp Aug 04 '18 at 07:02
  • @lopezdp - I think you was donwvoted not for question, but for post pictures. [Reason why not post images of code, data (or links to them)](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question) – jezrael Aug 04 '18 at 07:03
  • @lopezdp - Yes, next your question should be better, I believe. Btw, here is nice posts about [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) ;) – jezrael Aug 04 '18 at 07:10
  • it's weird. on the first run your answer worked. Now I am rerunning and I am getting this error: pred.index = ax.index AttributeError: 'AxesSubplot' object has no attribute 'index' any ideas? – lopezdp Aug 04 '18 at 07:17
  • @lopezdp - Sure, I think problem is `ax`, is is name of DataFrame or of `axessublot`? Maybe try rename dataframe to `df` from `ax` ;) – jezrael Aug 04 '18 at 07:19
0
df.index = pd.PeriodIndex(df.Date, freq='D')
Buddy
  • 10,874
  • 5
  • 41
  • 58
Coder
  • 15
  • 1
  • 7