w3resource

Python Pandas: Extract items at given positions of a given series

Python Pandas: Data Series Exercise-22 with Solution

Write a Pandas program to extract items at given positions of a given series.

Sample Solution :

Python Code :

import pandas as pd
num_series = pd.Series(list('2390238923902390239023'))
element_pos = [0, 2, 6, 11, 21]
print("Original Series:")
print(num_series)
result = num_series.take(element_pos)
print("\nExtract items at given positions of the said series:")
print(result)

Sample Output:

Original Series:
0     2
1     3
2     9
3     0
4     2
5     3
6     8
7     9
8     2
9     3
10    9
11    0
12    2
13    3
14    9
15    0
16    2
17    3
18    9
19    0
20    2
21    3
dtype: object

Extract items at given positions of the said series:
0     2
2     9
6     8
11    0
21    3
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 find the positions of numbers that are multiples of 5 of a given series.
Next: Write a Pandas program to get the positions of items of a given series in another 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.