Pandas Stack Two Series Vertically and Horizontally

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.

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 example

# 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

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

Alternatively, 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

Use ignore_index=True param to reset the index on the combined 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 of Stack Two Series Vertically and Horizontally


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)

8. 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

References

Leave a Reply

You are currently viewing Pandas Stack Two Series Vertically and Horizontally