How to get few rows from a Series in Pandas?

PandasServer Side ProgrammingProgramming

Indexing in pandas Series is used to access elements from a series, in the same way, we can also get a set of values from a series object using the slicing technique. Nothing but we can retrieve the subset from a pandas Series object. This process is similar to the NumPy array slicing.

Retrieving some set of elements can be achieved by defining the start and endpoint index values of a series.

Example

# importing required packages
import pandas as pd
import numpy as np

# creating pandas Series object
series = pd.Series(np.random.rand(10))

print(series)

print('\nRetrieve first 3 elements')
# accessing elements by using index values
print(series[0:3])

print('\nRetrieve 3 elements from middle')
# accessing elements by using index values
print(series[4:7])

Explanation

In this example, we have created a Series object with positional index values, and the data is random values generated by using the NumPy random module.

Here, we have retrieved a set of rows from our series object by defining the start and end values with the series name “series[start:end]”.

Output

0   0.445380
1   0.644535
2   0.961340
3   0.242597
4   0.272414
5   0.126234
6   0.579381
7   0.358938
8   0.542832
9   0.476023
dtype: float64

Retrieve first 3 elements
0   0.445380
1   0.644535
2   0.961340
dtype: float64

Retrieve 3 elements from middle
4   0.272414
5   0.126234
6   0.579381
dtype: float64

Output

The first part of the above block is representing the entire series object and second part is sub set values sliced by defining index range from 0 to 3 (series[0:3]), in the same way we have retrieved an another sub set by defining index range between 4 to 7 (series[4:7]) which is represented in last part of the above code block.

Example

We can also slice labele indexed series object, This following example will explain how to slice a series having labele indexes.

# importing required packages
import pandas as pd

# creating pandas Series object
series = pd.Series({'B':'black', 'W':'white','R':'red', 'Bl':'blue','G':'green'})
print(series)

print('\nRetrieve first 2 elements')
# accessing elements by using index values
print(series[0:2])

print('\nRetrieve elements by using label index')

# accessing elements by using label index
print(series['W':'G'])

Explanation

In this above example, we have created a series object with label indexes and we have applied the slicing using positional index values as well as label index values.

Accessed the 2 elements from the series by specifying position index from 0 to 2 (series[0:2]). In the same way, we have specified the label indexes also (like series[‘W’:’G’]).

Output

B   black
W   white
R     red
Bl   blue
G   green
dtype: object

Retrieve first 2 elements
B   black
W   white
dtype: object

Retrieve elements by using label index
W   white
R     red
Bl   blue
G   green
dtype: object

The above block is the output of an entire series object and some few rows accessed by using both positional indexing and label indexing on our label indexed series object.

raja
Updated on 17-Nov-2021 07:50:20

Advertisements