0

I have prepared an example:

from datetime import datetime, timedelta
import pandas as pd

now= datetime.now()
time1= pd.Timestamp(now.year, now.month, now.day)- timedelta(days=1)
time2= pd.Timestamp(now.year, now.month, now.day)- timedelta(days=2)

data1= {time1:1}
data2= {time2:2}

series1 =pd.Series(data1)
series2 =pd.Series(data2)

print(series1)
print(series2)

merged= pd.DataFrame()
merged= pd.concat(series1,series2)
print(merged)

I have two pandas series, series1&2 and I want to merge them into one single pandas dataframe despite having different keys (dates). So the final dataframe simply has 2 columns with the name of the series and one row below holding the data ie 1 and 2.

I have tried to concat the two series but can't since the keys are different.

Desired columns:

column1, column2

Desired data beneath columns:

1,2

ie the key(date) is completely removed and we have a dataframe with defined column names with only the data from the two pandas series

wjandrea
  • 28,235
  • 9
  • 60
  • 81
daniel stafford
  • 241
  • 1
  • 8
  • What have you tried? What do you need help with exactly? Like to start, are you familiar with `pd.concat`? – wjandrea Aug 04 '23 at 21:56
  • What's the final df supposed to look like? Should it have the dates as the index or as the columns? Please [edit] to add it. For reference, see [mre] and [How to make good reproducible pandas examples](/q/20109391/4518341). – wjandrea Aug 04 '23 at 22:01
  • edited, hopefully this helps but can do more if this helps – daniel stafford Aug 04 '23 at 23:16
  • 1
    Does this answer your question? [pandas concat columns ignore_index doesn't work](/q/32801806/4518341). It's a few steps beyond your code, but I'm trying to skip to the chase cause your code doesn't work. Specifically, `pd.concat([series1.reset_index(drop=True), series2.reset_index(drop=True)], axis=1)` – wjandrea Aug 04 '23 at 23:51
  • Both work exactly as expected. What if I had a for loop that creates theses pandas series {Time:Value} but each loop has a different Time, how could I merge these into a single dataframe? ie not just a known series1 and series2? I have updated the question. – daniel stafford Aug 05 '23 at 10:27
  • Don't change the question; it invalidates previous comments and suggestions. I've rolled back your edit. You could ask a new question, but I think you'll learn more if you figure this out for yourself. It's not that hard. `AttributeError: 'dict' object has no attribute 'reset_index'` -- Think about that. A dict doesn't have a `reset_index` method, so what does? What do you actually want to run `reset_index` on? How do you run a method on multiple objects and make a list out of that? – wjandrea Aug 05 '23 at 16:42

0 Answers0