w3resource

Pandas Data Series: Add some data to an existing Series

Pandas: Data Series Exercise-12 with Solution

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

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/pandas/python-pandas-data-series-exercise-12.php