NumPy: Extract all the rows from a given array where a specific column starts with a given character
Extract rows based on column starting character.
Write a NumPy program to extract all the rows from a given array where a specific column starts with a given character.
Sample array:
[['01' 'V' 'Debby Pramod']
['02' 'V' 'Artemiy Ellie']
['03' 'V' 'Baptist Kamal']
['04' 'V' 'Lavanya Davide']
['05' 'V' 'Fulton Antwan']
['06' 'V' 'Euanthe Sandeep']
['07' 'V' 'Endzela Sanda']
['08' 'V' 'Victoire Waman']
['09' 'V' 'Briar Nur']
['10' 'V' 'Rose Lykos']]
Sample Solution:
Python Code:
Sample Output:
Original array: [['01' 'V' 'Debby Pramod'] ['02' 'V' 'Artemiy Ellie'] ['03' 'V' 'Baptist Kamal'] ['04' 'V' 'Lavanya Davide'] ['05' 'V' 'Fulton Antwan'] ['06' 'V' 'Euanthe Sandeep'] ['07' 'V' 'Endzela Sanda'] ['08' 'V' 'Victoire Waman'] ['09' 'V' 'Briar Nur'] ['10' 'V' 'Rose Lykos']] Student name starting with E : [['06' 'V' 'Euanthe Sandeep'] ['07' 'V' 'Endzela Sanda']] Student id starting with 1 : [['10' 'V' 'Rose Lykos']]
For more Practice: Solve these Related Problems:
- Write a NumPy program to filter rows from a 2D array of strings where a specific column’s value starts with a given character using np.char.startswith.
- Create a function that returns the indices of rows whose specified column matches a given starting letter, employing vectorized string operations.
- Implement a solution that uses a boolean mask based on np.char.startswith to extract and return the matching rows.
- Test the extraction function on arrays with varying cases to ensure the filtering can be adjusted for case sensitivity.
Go to:
PREV : Shuffle specific rows in a 11x3 student data array.
NEXT : Compute weight sum of rows where names match a condition.
Python-Numpy Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.