How to get column index from column name in Python Pandas?

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 01:53:24

13K+ Views

To get column index from column name in Python Pandas, we can use the get_loc() method on the DataFrame's columns Index object. Syntax df.columns.get_loc(column_name) Parameters column_name − The name of the column whose index you want to find Return Value Returns an integer representing the positional index of the specified column. Example Let's create a DataFrame and find the index of different columns ? import pandas as pd # Create a DataFrame df = pd.DataFrame( { ... Read More

Python program to construct Equidigit tuples

AmitDiwan
Updated on 26-Mar-2026 01:53:08

163 Views

Equi-digit tuples are tuples where a number is split into two halves at the middle position. Python provides an efficient way to construct these using the floor division operator // and string slicing. Understanding Equi-digit Tuples An equi-digit tuple divides a number into two parts of equal or nearly equal length. For even-digit numbers, both parts have equal digits. For odd-digit numbers, the first part has one fewer digit than the second part. Example Below is a demonstration of constructing equi-digit tuples ? my_list = [5613, 1223, 966143, 890, 65, 10221] print("The list ... Read More

Python program to omit K length Rows

AmitDiwan
Updated on 26-Mar-2026 01:52:54

263 Views

When working with lists of lists, you may need to filter out rows based on their length. This Python program demonstrates how to omit rows that have exactly K elements using iteration and the len() method. Example Below is a demonstration of the same − my_list = [[41, 7], [8, 10, 12, 8], [10, 11], [6, 82, 10]] print("The list is :") print(my_list) my_k = 2 print("The value of K is") print(my_k) my_result = [] for row in my_list: if len(row) != my_k : ... Read More

Python program to extract rows with common difference elements

AmitDiwan
Updated on 26-Mar-2026 01:52:40

183 Views

When it is required to extract rows with common difference elements (arithmetic progression), we can iterate through each row and check if consecutive elements have the same difference. A flag value is used to track whether a row maintains constant difference throughout. Understanding Common Difference A sequence has common difference when the difference between consecutive elements remains constant. For example, in [11, 12, 13], the difference is always 1. Example Below is a demonstration of extracting rows with common difference − data_list = [[31, 27, 10], [8, 11, 12], [11, 12, 13], [6, 9, ... Read More

Python – Test if tuple list has a single element

AmitDiwan
Updated on 26-Mar-2026 01:52:25

380 Views

When it is required to test if a tuple list contains a single unique element across all tuples, we can use different approaches. This means checking if all elements in all tuples are the same value. Using Nested Loop with Flag The traditional approach uses nested loops with a flag variable to track whether all elements are identical ? my_list = [(72, 72, 72), (72, 72), (72, 72)] print("The list is :") print(my_list) my_result = True for sub in my_list: flag = True for element in ... Read More

Python – Redistribute Trimmed Values

AmitDiwan
Updated on 26-Mar-2026 01:52:08

129 Views

When you need to redistribute trimmed values, you remove elements from both ends of a list and distribute their sum evenly across the remaining elements. This technique uses list slicing and the division operator. What is Redistributing Trimmed Values? Redistributing trimmed values means: Remove a specified number of elements from both ends of a list Calculate the sum of the removed (trimmed) elements Distribute this sum evenly among the remaining elements Example Below is a demonstration of redistributing trimmed values ? my_list = [11, 26, 24, 75, 96, 37, 48, 29, 93] ... Read More

Python – Consecutive identical elements count

AmitDiwan
Updated on 26-Mar-2026 01:51:48

1K+ Views

When working with lists in Python, you might need to count how many distinct values appear consecutively (repeated adjacent elements). This can be achieved using iteration, the append() method, and set() to find unique consecutive elements. Example Here's how to count distinct consecutive identical elements in a list − my_list = [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): if my_list[index] == my_list[index + 1]: ... Read More

Python – Rows with all List elements

AmitDiwan
Updated on 26-Mar-2026 01:51:33

824 Views

When it is required to find rows that contain all elements from a given list, a flag value, simple iteration and the 'append' method can be used. This is useful for filtering data based on multiple criteria. Example Below is a demonstration of finding rows containing all specified elements ? my_list = [[8, 6, 3, 2], [1, 6], [2, 1, 7], [8, 1, 2]] print("The list is :") print(my_list) sub_list = [1, 2] result = [] for row in my_list: flag = True ... Read More

Python – Filter rows without Space Strings

AmitDiwan
Updated on 26-Mar-2026 01:51:16

283 Views

When working with nested lists containing strings, you might need to filter out rows that contain any strings with spaces. Python provides several approaches to accomplish this task using list comprehensions and string checking methods. Using Regular Expressions The most robust approach uses the re module to search for whitespace characters ? import re data = [["python is", "fun"], ["python", "good"], ["python is cool"], ["love", "python"]] print("The original list is:") print(data) result = [row for row in data if not any(bool(re.search(r"\s", element)) for element in row)] print("Rows without space strings:") print(result) ... Read More

Python – Equidistant consecutive characters Strings

AmitDiwan
Updated on 26-Mar-2026 01:50:59

301 Views

Equidistant consecutive character strings are strings where the ASCII difference between consecutive characters remains constant throughout the string. For example, in "abc", each character is 1 ASCII value apart, while in "agms", each character is 6 ASCII values apart. Understanding the Concept To check if a string has equidistant consecutive characters, we need to ? Calculate the ASCII difference between the first two characters Verify that all other consecutive pairs have the same difference Use ord() to get ASCII values and all() to check the condition Example Here's how to find all equidistant ... Read More

Advertisements