Combining multiple Series into a DataFrame in Pandas
Start your free 7-days trial now!
To combine multiple Series into a single DataFrame, use the concat(~)
method or the DataFrame(~)
constructor.
Combining Series horizontally
Series represent columns
To combine two Series horizontally:
Note the following:
each Series represents a column
the parameter
axis=1
forconcat(~)
is used to perform horizontal concatenation, as opposed to vertical.
Note that if your Series do not have exact matching index, the resulting DataFrame will have NaN
values:
Series represent rows
To combine two Series where each series represents a row:
Note that if your Series do not have exact matching index, the resulting DataFrame will have NaN
values:
Combining Series vertically
To combine two Series vertically to form a DataFrame:
Note that calling concat(~)
on two series with the default axis=0
results in a Series, and so we need to convert this Series into a DataFrame via the DataFrame constructor.