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.
Output: [3.23, 6.6 , 7.7 , 8.9 ]
Explanation: Extracting the 3rd column from 1D array of tuples.
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.
array([3.23, 6.6 , 7.7 , 8.9 ])
Method 2: Using the lambda function
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.
approval temperature 0 10 (18.18, 2.27, 3.23) 1 20 (36.43, 34.24, 6.6) 2 30 (5.25, 6.16, 7.7) 3 40 (7.37, 28.8, 8.9) 4 50 (12, 23, 3 The output for extracting 3rd column from the array of tuples [3.23 6.6 7.7 8.9 3. ]
Method 3: Using list comprehension
A list comprehension can be used to extract a particular element from each tuple in the list and create a new list with the extracted elements.
In this example, we have the following list of tuples, we can use a list comprehension to extract the second element from each tuple (index 1) as follows:
Time Complexity: O(n)
Space Complexity: O(1)
Please Login to comment...