w3resource

Python Pandas: Create a subset of a given series based on value and condition

Python Pandas: Data Series Exercise-13 with Solution

Write a Pandas program to create a subset of a given series based on value and condition.

Sample Solution :

Python Code :

import pandas as pd
s = pd.Series([0, 1,2,3,4,5,6,7,8,9,10])
print("Original Data Series:")
print(s)
print("\nSubset of the above Data Series:")
n = 6
new_s = s[s < n]
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
0       500
1       php
dtype: object                     

Python Code Editor:


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

Previous: Write a Pandas program to add some data to an existing Series.
Next: Write a Pandas program to change the order of index of a given series.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Membership Testing in a Collection

>>> a = ('one', 'two', 'three', 'four', 'five')
>>> if 'one' in a:
...     print('The tuple contains one.')
... 
The tuple contains one.
>>> b = {0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
>>> if 2 in b.keys():
...     print('The dict has the key of 2.')
... 
The dict has the key of 2.