In this python pandas program, we will extract items at given positions of a series using pandas.
Steps to solve the program
- Import pandas library as pd.
- Create a series using pd.Series().
- Extract items at given positions of a series using df.take(positions) where positions are the index no of values that we want to extract.
- Print the output.
import pandas as pd
df = pd.Series([3,0,3,2,2,0,3,3,2])
positions = [1,4,7]
print("Original Series:")
print(df)
result = df.take(positions)
print("Extract items at given positions of the said series:")
print(result)
Output :
Original Series:
0 3
1 0
2 3
3 2
4 2
5 0
6 3
7 3
8 2
dtype: int64
Extract items at given positions of the said series:
1 0
4 2
7 3
dtype: int64