How to use Slice In Python Pandas

As a first step let us first define a 1D array of tuples with each tuple having 3 elements, if we consider these 3 elements as 3 columns, we can use the slicing technique to extract a particular column.

Python3




import numpy as np
 
# define a 1d array of tuples
arr = np.array([(18.18, 2.27, 3.23), (36.43, 34.24, 6.6),
                (5.25, 6.16, 7.7), (7.37, 28.8, 8.9)])
 
# slice the array by passing the
# column number
arr[:, 2]


Output:

array([3.23, 6.6 , 7.7 , 8.9 ])

How to extract a particular column from 1D array of tuples?

In this article, we will cover how to extract a particular column from a 1-D array of tuples in python.

Example 

Input:  [(18.18,2.27,3.23),(36.43,34.24,6.6),(5.25,6.16,7.7),(7.37,28.8,8.9)]

Output: [3.23, 6.6 , 7.7 , 8.9 ]

Explanation: Extracting the 3rd column from 1D array of tuples.

Similar Reads

Method 1: Using Slice

As a first step let us first define a 1D array of tuples with each tuple having 3 elements, if we consider these 3 elements as 3 columns, we can use the slicing technique to extract a particular column....

Method 2: Using the lambda function

...

Method 3: Using list comprehension

In this example, we are taking a pandas data frame and one of the columns is an array of tuples, we can slice that particular column and apply a lambda function to extract a particular column from the tuple of an array....

Contact Us