w3resource

Pandas Data Series: Add some data to an existing Series


12. Append Data to Series

Write a Pandas program to add some data to an existing Series.

Sample Solution:

Python Code:

import pandas as pd
s = pd.Series(['100', '200', 'python', '300.12', '400'])
print("Original Data Series:")
print(s)
print("\nData Series after adding some data:")
new_s = pd.concat([s, pd.Series([500, "php"])], ignore_index=True)
print(new_s)

Sample Output:

Original Data Series:
0       100
1       200
2    python
3    300.12
4       400
dtype: object

Data Series after adding some data:
0       100
1       200
2    python
3    300.12
4       400
5       500
6       php
dtype: object               

Explanation:

In the above exercise -

s = pd.Series(['100', '200', 'python', '300.12', '400']): This code creates a Pandas Series object 's' containing a sequence of five string values: ['100', '200', 'python', '300.12', '400'].

new_s = pd.concat([s, pd.Series([500, "php"])], ignore_index=True): This line creates a new Pandas Series object 'new_s' by concatenating the original Series object 's' with a new Pandas Series object containing two additional values: [500, "php"]. The pd.concat() function is used to concatenate the two Series objects, with the 'ignore_index' parameter set to True to reset the index of the resulting Series object 'new_s'.


For more Practice: Solve these Related Problems:

  • Write a Pandas program to append a new Series to an existing Series and remove duplicates.
  • Write a Pandas program to add a scalar value to an existing Series and then compute the cumulative sum.
  • Write a Pandas program to add new elements to a Series at specified positions using index reordering.
  • Write a Pandas program to concatenate two Series with overlapping indexes and then fill missing values.

Python-Pandas Code Editor:

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

Previous: Write a Pandas program to convert Series of lists to one Series.
Next: Write a Pandas program to create a subset of a given series based on value and condition.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.