Pandas Series.append()
0 1308
Pandas Series.append() Method
The Series.append() method in pandas is used to concatenate two or more Series objects, returning a new Series object. This method is particularly useful when you need to combine multiple Series or add new rows to an existing Series.
Syntax
Series.append(to_append, ignore_index=False, verify_integrity=False)
Parameters:
to_append: Series, or list/tuple of Series. The Series to append to the original Series.ignore_index: bool, default False. If True, do not use the index labels. The resulting Series will have a new integer index.verify_integrity: bool, default False. If True, checks whether the new concatenated axis contains duplicates. Raises ValueError if duplicates are found.
Example 1: Appending One Series to Another
Consider two Series:
import pandas as pd
# Creating the first Series
sr1 = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio'])
# Creating the second Series
sr2 = pd.Series(['Chicago', 'Shanghai', 'Beijing', 'Jakarta', 'Seoul'])
# Appending sr2 to sr1
result = sr1.append(sr2, ignore_index=True)
print(result)
Output:
0 New York
1 Chicago
2 Toronto
3 Lisbon
4 Rio
5 Chicago
6 Shanghai
7 Beijing
8 Jakarta
9 Seoul
dtype: object
In this example, sr2 is appended to sr1, and the ignore_index=True parameter ensures that the index is reset in the resulting Series.
Example 2: Appending a List to a Series
You can also append a list to a Series:
import pandas as pd
# Creating a Series
sr1 = pd.Series(['New York', 'Chicago', 'Toronto'])
# Creating a list
new_data = ['Lisbon', 'Rio']
# Appending the list to the Series
sr1 = sr1.append(pd.Series(new_data), ignore_index=True)
print(sr1)
Output:
0 New York
1 Chicago
2 Toronto
3 Lisbon
4 Rio
dtype: object
Here, a list of new data is converted to a Series and appended to sr1.
Example 3: Appending a Dictionary as a Row
If you have a dictionary and want to append it as a new row:
import pandas as pd
# Creating a Series
sr1 = pd.Series(['New York', 'Chicago', 'Toronto'])
# Creating a dictionary
new_data = {'0': 'Lisbon', '1': 'Rio'}
# Appending the dictionary to the Series
sr1 = sr1.append(pd.Series(new_data), ignore_index=True)
print(sr1)
Output:
0 New York
1 Chicago
2 Toronto
3 Lisbon
4 Rio
dtype: object
In this case, the dictionary is converted to a Series and appended to sr1.
Important Notes
- The
append()method does not modify the original Series; it returns a new Series with the appended data. - As of pandas version 2.0, the
append()method has been removed. It is recommended to usepd.concat()for appending data to a Series.
Conclusion
The Series.append() method is a convenient way to add data to a Series. However, with its removal in pandas 2.0, transitioning to pd.concat() is advisable for future-proofing your code. Understanding these methods enhances your ability to manipulate and analyze data efficiently in pandas.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments