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 into a DataFrame in Pandas

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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:

``` s1 = pd.Series([3,4], index=["a","b"])   # represents a columns2 = pd.Series([5,6], index=["a","b"])pd.concat([s1,s2], axis=1) 0 1a 3 5b 4 6 ```

Note the following:

• each Series represents a column

• the parameter `axis=1` for `concat(~)` 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:

``` s1 = pd.Series([3,4], index=["a","b"])s2 = pd.Series([5,6], index=["b","c"])pd.concat([s1,s2], axis=1) 0 1a 3.0 NaNb 4.0 5.0c NaN 6.0 ```

## Series represent rows

To combine two Series where each series represents a row:

``` s1 = pd.Series([3,4], index=["a","b"])   # represents a rows2 = pd.Series([5,6], index=["a","b"])pd.DataFrame([s1,s2]) a b0 3 41 5 6 ```

Note that if your Series do not have exact matching index, the resulting DataFrame will have `NaN` values:

``` s1 = pd.Series([3,4], index=["a","b"])s2 = pd.Series([5,6], index=["b","c"])pd.DataFrame([s1,s2])    a    b    c0  3.0  4.0  NaN1  NaN  5.0  6.0 ```

# Combining Series vertically

To combine two Series vertically to form a DataFrame:

``` s1 = pd.Series([3,4], index=["a","b"])s2 = pd.Series([5,6], index=["a","b"])pd.DataFrame(pd.concat([s1,s2])) 0a 3b 4a 5b 6 ```

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.

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!