search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# Combining multiple Series to form a DataFrame in Pandas

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

To combine multiple Series to form a DataFrame in Pandas, use the `concat(~)` method.

# Horizontally stacking Series

To horizontally stack two Series:

``` s1 = pd.Series(['a','b'])s2 = pd.Series(['c','d'])df = pd.concat([s1, s2], axis=1) # returns a DataFramedf 0 10 a c1 b d ```

Here, both `s1` and `s2` share the same index (`[0,1]`) so they could be stacked horizontally (`axis=1`).

## Case when index labels do not align

In the case when the index labels don't perfectly align, we would end up with missing values:

``` s1 = pd.Series(['a','b'], index=["A","B"])s2 = pd.Series(['c','d'], index=["B","C"])pd.concat([s1, s2], axis=1) 0 1A a NaNB b cC NaN d ```

## Supplement - assigning new column labels

The resulting DataFrame will have the default integer indices as the column labels, so you may want to assign new labels like so:

``` df.columns = ["A","B"]df A Ba a NaNb b cc NaN d ```

# Vertically stacking Series

To vertically stack multiple Series to form a new DataFrame, use `concat(~)`:

``` s1 = pd.Series(['a','b'])s2 = pd.Series(['c','d'])df = pd.concat([s1, s2], axis=0).to_frame()df 00 a1 b0 c1 d ```

Note the following:

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
2
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!