pandas.concat()
function is used to stack two given series vertically and horizontally in pandas. When you concat()
two pandas Series along with row-wise, it creates a new Series where the elements are all rows of two Series, basically it does append one Series with another. In this article, I will explain how to stack two pandas series vertically and horizontally with examples.
Key Points –
- Pandas provides functionality to stack two Series vertically and horizontally using various functions.
- The
pd.concat()
function is commonly used to stack Series vertically by concatenating them along a specified axis. - To stack Series horizontally, the
pd.concat()
function can also be used by specifyingaxis=1
. - The
pd.DataFrame()
constructor can stack two Series horizontally by passing them as columns in a dictionary.
1. Quick Examples of Stack Two Series Vertically and Horizontally
If you are in a hurry, below are some quick examples of how to stack two pandas Series vertically and horizontally.
# Below are a quick examples.
# Example 1: Stack two series horizontally
# Using pandas.concat() function
ser2 = pd.concat([ser, ser1], axis = 1)
# Example 2: Stack two series vertically
# Using pandas.concat() function
ser2 = pd.concat([ser, ser1], axis = 0)
# Example 3: Stack two series vertically
# Using Series.append() function
ser2 = ser.append(ser1)
# Example 4: Use pandas.concat() & ignore_index=True
ser2 = pd.concat([ser, ser1], ignore_index=True)
2. Create Two Pandas Series
Pandas Series is a one-dimensional, Index-labeled data structure available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.
Now, let’s create two pandas series using a list of values.
import pandas as pd
# Create two pandas series
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"])
ser1 = pd.Series([20000,25000,23000,28000,55000,23000,28000])
print(ser)
print(ser1)
Yields below output.
# Output:
0 Java
1 Spark
2 PySpark
3 Pandas
4 NumPy
5 Python
6 Oracle
dtype: object
0 20000
1 25000
2 23000
3 28000
4 55000
5 23000
6 28000
dtype: int64
3. Stack Two Pandas Series Horizontally
pandas.concat()
function is used to stack two pandas Series horizontally. For that, we need to pass axis=1
along with a list of series. If you wanted to concatenate two pandas DataFrame columns refer pandas.concate() function.
# Stack two series horizontally
# Using pandas.concat() function
ser2 = pd.concat([ser, ser1], axis = 1)
print(ser2)
Yields below output.
# Output:
0 1
0 Java 20000
1 Spark 25000
2 PySpark 23000
3 Pandas 28000
4 NumPy 55000
5 Python 23000
6 Oracle 28000
4. Stack Two Pandas Series Vertically
Alternatively, you can also stack two pandas Series vertically by passing axis=0
parameter into pandas.concat()
function.
# Stack two series vertically using pandas.concat() function
ser2 = pd.concat([ser, ser1], axis = 0)
print(ser2)
Yields below output.
# Output:
0 Java
1 Spark
2 PySpark
3 Pandas
4 NumPy
5 Python
6 Oracle
0 20000
1 25000
2 23000
3 28000
4 55000
5 23000
6 28000
dtype: object
5. Stack Two Series Vertically Using Series.append() Function
Similarly, you can also use Series.append() function to stack two series vertically. For E.x, ser.append(ser1) appends ser1
to the ser
Series. If you want to append two pandas DataFrame refer to append() function.
# Stack two series append() vertically
ser2 = ser.append(ser1)
print(ser2)
Yields the same output as above.
6. Use pandas.concat() & ignore_index=True
Using pandas.concat()
with ignore_index=True
to reset the index on the combined Series. For instance, pd.concat()
vertically stacks ser
and ser1
, and the resulting Series ser2
has a new continuous index starting from 0, ignoring the original indices of the input Series.
# Use pandas.concat() & ignore_index=True
ser2 = pd.concat([ser, ser1], ignore_index=True)
print(ser2)
Yields below output.
# Output:
0 Java
1 Spark
2 PySpark
3 Pandas
4 NumPy
5 Python
6 Oracle
7 20000
8 25000
9 23000
10 28000
11 55000
12 23000
13 28000
dtype: object
7. Complete Examples
import pandas as pd
# Create the Series
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"])
ser1 = pd.Series([20000,25000,23000,28000,55000,23000,28000])
print(ser)
print(ser1)
# Stack two series horizontally
# Using pandas.concat() function
ser2 = pd.concat([ser, ser1], axis = 1)
print(ser2)
# Stack two series vertically
# Using pandas.concat() function
ser2 = pd.concat([ser, ser1], axis = 0)
print(ser2)
# Stack two series vertically
# Using Series.append() function
ser2 = ser.append(ser1)
print(ser2)
# Use pandas.concat() & ignore_index=True
ser2 = pd.concat([ser, ser1], ignore_index=True)
print(ser2)
Frequently Asked Questions on Stack Two Series Vertically and Horizontally
To stack two Series vertically means combining them into a single Series with one stacked on top of the other. This operation increases the length of the resulting Series.
You can stack two Series vertically using the pd.concat()
function from the Pandas library. By default, pd.concat()
concatenates along the vertical axis (axis=0).
When you set ignore_index=True
with pd.concat()
, it creates a new index for the resulting Series, ignoring the original indices of the input Series. This is useful when you want a continuous index for the stacked Series.
To stack two Series horizontally, you can use pd.concat()
with the axis=1
parameter. This concatenates the Series side by side, increasing the width of the resulting DataFrame.
Conclusion
In this article, I have explained how to stack two given series vertically and horizontally in pandas using pandas.concat()
, Series.append()
functions with examples.
Happy Learning !!
Related Articles
- Get Series Index in Pandas
- Pandas Series.clip() Function
- Pandas Series iloc[] Function
- How to Append Pandas Series?
- How to Rename a Pandas Series
- Pandas series.str.get() Function
- Pandas Series round() Function
- Pandas Series.dtype() Function
- Pandas Series Drop duplicates() Function
- How to Make a Histogram in Pandas Series?
- Change the Index Order in Pandas Series
- Convert Series to Dictionary(Dict) in Pandas
- Convert Pandas Series to NumPy Array
- How to Convert NumPy Array to Pandas Series