Ben
Ben

Reputation: 1688

Add pandas Series to a DataFrame, preserving index

I have been having some problems adding the contents of a pandas Series to a pandas DataFrame. I start with an empty DataFrame, initialised with several columns (corresponding to consecutive dates).

I would like to then sequentially fill the DataFrame using different pandas Series, each one corresponding to a different date. However, each Series has a (potentially) different index.

I would like the resulting DataFrame to have an index that is essentially the union of each of the Series indices.

I have been doing this so far:

for date in dates:
     df[date] = series_for_date

However, my df index corresponds to that of the first Series and so any data in successive Series that correspond to an index 'key' not in the first Series are lost.

Any help would be much appreciated!

Ben

Upvotes: 2

Views: 2890

Answers (1)

JAB
JAB

Reputation: 12799

If i understand you can use concat:

pd.concat([series1,series2,series3],axis=1)

Upvotes: 1

Related Questions