26 Oct Append two Pandas Series
In this lesson, we will append two Pandas series using the append() method. This will append two series. With that, the ignore_index parameter of the append() will allow you to ignore or consider the index. If ignore_index is set to True, the original indexes are ignored and replaced by 0, 1, 2, etc. in the output. The default is False.
Note: The append() method deprecated since version Pandas 1.4.0.
We will see two examples:
- Append two Pandas series considering the original index
- Append two Pandas series ignoring the original index
Append two Pandas series considering the original index
To append two series, use the append() method. The ignore_index parameter is by default set to False. This will keep both series indexes alive even after the append. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import pandas as pd # Data to be stored in the Pandas Series data1 = [10, 20, 40, 80, 100] data2 = [150, 200] # Create two Series using the Series() method series1 = pd.Series(data1, index = ["RowA", "RowB", "RowC", "RowD", "RowE"]) series2 = pd.Series(data2, index = ["RowF", "RowG"]) # Display the Series print("Series1 (with custom index labels): \n",series1) print("\nSeries2 (with custom index labels): \n",series2) # Append # The ignore_index parameter is by default set to False: result = series1.append(series2, ignore_index = False) # Print the result print("\nResult after appending (considering original indexes):\n",result) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Series1 (with custom index labels): RowA 10 RowB 20 RowC 40 RowD 80 RowE 100 dtype: int64 Series2 (with custom index labels): RowF 150 RowG 200 dtype: int64 Result after appending (considering original indexes): RowA 10 RowB 20 RowC 40 RowD 80 RowE 100 RowF 150 RowG 200 dtype: int64 |
Append two Pandas series ignoring the original index
To append two series, use the append() method. For ignoring the original indexes and replacing them with 0, 1, 2, etc, set the ignore_index to True as discussed above.
Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import pandas as pd # Data to be stored in the Pandas Series data1 = [10, 20, 40, 80, 100] data2 = [150, 200] # Create two Series using the Series() method series1 = pd.Series(data1, index = ["RowA", "RowB", "RowC", "RowD", "RowE"]) series2 = pd.Series(data2, index = ["RowF", "RowG"]) # Display the Series print("Series1 (with custom index labels): \n",series1) print("\nSeries2 (with custom index labels): \n",series2) # Append # The ignore_index parameter is set to True for ignoring original indexes result = series1.append(series2, ignore_index = True) # Print the result print("\nResult after appending (ignoring original indexes):\n",result) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Series1 (with custom index labels): RowA 10 RowB 20 RowC 40 RowD 80 RowE 100 dtype: int64 Series2 (with custom index labels): RowF 150 RowG 200 dtype: int64 Result after appending (ignoring original indexes): 0 10 1 20 2 40 3 80 4 100 5 150 6 200 dtype: int64 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments