w3resource

Pandas Data Series: Stack two given series vertically and horizontally

 

Pandas: Data Series Exercise-37 with Solution

Write a Pandas program to stack two given series vertically and horizontally.

Sample Solution :

Python Code :

import pandas as pd
series1 = pd.Series(range(10))
series2 = pd.Series(list('pqrstuvwxy'))
print("Original Series:")
print(series1)
print(series2)
series1.append(series2)
df = pd.concat([series1, series2], axis=1)
print("\nStack two given series vertically and horizontally:")
print(df)

Sample Output:

Original Series:
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
dtype: int64
0    p
1    q
2    r
3    s
4    t
5    u
6    v
7    w
8    x
9    y
dtype: object

Stack two given series vertically and horizontally:
   0  1
0  0  p
1  1  q
2  2  r
3  3  s
4  4  t
5  5  u
6  6  v
7  7  w
8  8  x
9  9  y

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to convert given series into a dataframe with its index as another column on the dataframe.
Next: Write a Pandas program to check the equality of two given series.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Merging two dicts in Python 3.5+ with a single expression

Example:

# How to merge two dictionaries
# in Python 3.5+

x = {'p': 1, 'q': 3}
y = {'q': 5, 'r': 8}

z = {**x, **y}

z
{'r': 4, 'p': 1, 'q': 3}

z = dict(x, **y)
print(z)

Output:

{'p': 1, 'q': 5, 'r': 8}