Programming Articles

Page 120 of 2547

Python - Calculate the count of column values of a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

To calculate the count of column values in a Pandas DataFrame, use the count() method. This method returns the number of non-null values in each column, making it useful for data validation and analysis. Importing Required Library First, import the Pandas library − import pandas as pd Counting Values in a Specific Column You can count non-null values in a specific column by accessing the column and applying count() − import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { ...

Read More

Python - Index Directory of Elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 313 Views

When it is required to index directory of elements in a list, list comprehension along with set operator is used. This creates a dictionary mapping each unique element to all its index positions in the list. Syntax {key: [index for index, value in enumerate(list) if value == key] for key in set(list)} Example Below is a demonstration of creating an index directory ? my_list = [81, 36, 42, 57, 68, 12, 26, 26, 38] print("The list is :") print(my_list) my_result = {key: [index for index, value in enumerate(my_list) ...

Read More

Python - Convert List to custom overlapping nested list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 257 Views

When it is required to convert a list to a customized overlapping nested list, an iteration along with the 'append' method can be used. This technique creates sublists of a specified size with a defined step interval, allowing elements to overlap between consecutive sublists. Syntax for index in range(0, len(list), step): result.append(list[index: index + size]) Parameters The overlapping nested list conversion uses two key parameters: step − The interval between starting positions of consecutive sublists size − The length of each sublist Example Below is ...

Read More

Python Program to get indices of sign change in a list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 544 Views

When it is required to get indices of sign change in a list, a simple iteration along with append method can be used. A sign change occurs when consecutive numbers have different signs (positive to negative or negative to positive). Example Below is a demonstration of finding sign change indices ? my_list = [71, 24, -34, -25, -76, 87, 29, -60, 70, 8] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): # Check for sign change between current and next element ...

Read More

Python - Restrict Tuples by frequency of first element's value

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 264 Views

When working with lists of tuples, you may need to restrict tuples based on how frequently their first element appears. This technique is useful for filtering duplicates or limiting occurrences to a specific threshold. Example Below is a demonstration of restricting tuples by frequency of the first element ? my_list = [(21, 24), (13, 42), (11, 23), (32, 43), (25, 56), (73, 84), (91, 40), (40, 83), (13, 27)] print("The list is :") print(my_list) my_key = 1 my_result = [] mems = dict() for sub in my_list: if sub[0] not in mems.keys(): mems[sub[0]] = 1 else: mems[sub[0]] += 1 if mems[sub[0]]

Read More

Python - Find starting index of all Nested Lists

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 458 Views

When working with nested lists, you might need to find the starting index of each sublist if all elements were flattened into a single list. This is useful for indexing and data processing tasks. Understanding the Problem Given a nested list like [[51], [91, 22, 36, 44], [25, 25]], if we flatten it to [51, 91, 22, 36, 44, 25, 25], we want to know where each original sublist starts: [0, 1, 5]. Method 1: Using Simple Iteration Track the cumulative length as we iterate through each sublist ? my_list = [[51], [91, 22, ...

Read More

Python - Check if list contain particular digits

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 434 Views

When you need to check if all numbers in a list are composed only of specific digits, you can use string operations and set comparison. This is useful for validating numbers that should only contain certain digits. Example Below is a demonstration of checking if list elements contain only particular digits − numbers = [415, 133, 145, 451, 154] print("The list is:") print(numbers) allowed_digits = [1, 4, 5, 3] # Convert allowed digits to string set for faster lookup digit_string = ''.join([str(digit) for digit in allowed_digits]) all_numbers = ''.join([str(num) for num in numbers]) ...

Read More

Python - Total equal pairs in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 695 Views

When it is required to find the total equal pairs in a list, we can use the set() function along with the floor division operator // and iteration. This approach counts how many pairs can be formed from duplicate elements in the list. Example Below is a demonstration of finding equal pairs in a list − numbers = [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] print("The list is :") print(numbers) unique_elements = set(numbers) total_pairs = 0 for element in unique_elements: total_pairs ...

Read More

Python program to return rows that have element at a specified index

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 242 Views

When it is required to return rows that have an element at a specified index, a simple iteration and the append() function can be used. This technique is useful for filtering rows based on matching elements at specific positions. Basic Example Below is a demonstration of comparing elements at a specific index ? my_list_1 = [[21, 81, 35], [91, 14, 0], [64, 61, 42]] my_list_2 = [[21, 92, 63], [80, 19, 65], [54, 65, 36]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_key = 0 my_result = [] ...

Read More

Python Program to find the Next Nearest element in a Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 223 Views

Finding the next nearest element in a matrix involves searching for a specific value that appears after a given position. This is useful in applications like pathfinding, data analysis, and grid-based algorithms where you need to locate the next occurrence of a value. Algorithm The approach starts from a given position (x, y) and searches row by row for the target element. It first checks the remaining elements in the current row, then moves to subsequent rows. Example Below is a demonstration of finding the next nearest element ? def get_nearest_elem(matrix, x, y, target): ...

Read More
Showing 1191–1200 of 25,469 articles
« Prev 1 118 119 120 121 122 2547 Next »
Advertisements