Programming Articles

Page 346 of 2547

How to do groupby on a multiindex in Pandas?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 480 Views

A MultiIndex DataFrame in Pandas has multiple levels of row or column indices. You can perform groupby operations on specific levels of the MultiIndex using the level parameter or by referencing index names directly. Creating Sample Data Let's create a sample sales dataset to demonstrate groupby operations on MultiIndex ? import pandas as pd # Create sample sales data data = { 'Car': ['BMW', 'Mercedes', 'Lamborgini', 'Audi', 'Mercedes', 'Porsche', 'RollsRoyce', 'BMW'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Bangalore', 'Hyderabad', 'Mumbai', 'Mumbai', 'Delhi'], 'UnitsSold': [95, 80, ...

Read More

Python program to find the Decreasing point in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 288 Views

When it is required to find the decreasing point in a list, a simple iteration and the break statement are used. The decreasing point is the first index where an element is greater than its next element. Example Below is a demonstration of the same − my_list = [21, 62, 53, 94, 55, 66, 18, 1, 0] print("The list is :") print(my_list) my_result = -1 for index in range(0, len(my_list) - 1): if my_list[index + 1] < my_list[index]: my_result = index ...

Read More

Python – Sort by Rear Character in Strings List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 237 Views

When working with string lists in Python, you may need to sort them based on their last character. Python provides multiple approaches to achieve this using the sort() method with a custom key function. Using a Custom Function Define a function that returns the last character using negative indexing ? def get_rear_position(element): return element[-1] my_list = ['python', 'is', 'fun', 'to', 'learn'] print("The list is :") print(my_list) my_list.sort(key=get_rear_position) print("The result is :") print(my_list) The list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : ...

Read More

Python – Sort Matrix by None frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 208 Views

When it is required to sort a matrix by None frequency, we can define a helper function that counts None values in each row using list comprehension and the not operator. The matrix is then sorted based on the count of None values in ascending order. Example Below is a demonstration of sorting a matrix by None frequency − def get_None_freq(row): return len([element for element in row if not element]) my_list = [[None, 24], [None, 33, 3, None], [42, 24, 55], [13, None, 24]] print("The list is:") print(my_list) my_list.sort(key=get_None_freq) ...

Read More

Python – Extract range of Consecutive similar elements ranges from string list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 355 Views

When working with lists containing consecutive similar elements, you often need to extract ranges showing where each group of identical elements starts and ends. Python provides a simple approach using iteration and the append() method to identify these consecutive groups. Example Below is a demonstration of extracting consecutive similar element ranges ? my_list = [12, 23, 23, 23, 48, 48, 36, 17, 17] print("The list is:") print(my_list) my_result = [] index = 0 while index < (len(my_list)): start_position = index val = my_list[index] ...

Read More

Python – Filter Tuples with Strings of specific characters

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 395 Views

When it is required to filter tuples with strings that have specific characters, a list comprehension and the all() function can be used to check if all characters in each string exist within a given character set. Example Below is a demonstration of filtering tuples containing only strings whose characters are present in a specific character set − my_list = [('pyt', 'best'), ('pyt', 'good'), ('fest', 'pyt')] print("The list is :") print(my_list) char_string = 'pyestb' my_result = [index for index in my_list if all(all(sub in char_string for sub in element) for element in index)] ...

Read More

Python – Filter rows with Elements as Multiple of K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 235 Views

When working with nested lists in Python, you might need to filter rows where all elements are multiples of a specific value K. This can be achieved using list comprehension combined with the all() function and modulus operator. Syntax The general syntax for filtering rows with elements as multiples of K is − result = [row for row in nested_list if all(element % K == 0 for element in row)] Example Below is a demonstration of filtering rows where all elements are multiples of K − my_list = [[15, 10, 25], ...

Read More

Python – Character indices Mapping in String List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 728 Views

When working with string lists, you may need to map each character to the indices where it appears. Python provides an efficient solution using defaultdict from the collections module combined with enumeration and set operations. Syntax from collections import defaultdict result = defaultdict(set) for index, string in enumerate(string_list): for char in string.split(): result[char].add(index + 1) Example Below is a demonstration that maps each character to its string positions ? from collections import defaultdict my_list = ['p y t ...

Read More

Python – Extract dictionaries with values sum greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 564 Views

Sometimes we need to filter a list of dictionaries based on the sum of their values. This is useful when working with data where you want to find records whose total values exceed a certain threshold. Using Loop Iteration The most straightforward approach is to iterate through each dictionary and calculate the sum of its values ? student_scores = [ {"Math": 14, "Science": 18, "English": 19}, {"Math": 12, "Science": 4, "English": 16}, {"Math": 13, "Science": 17, "English": 11}, {"Math": 13, ...

Read More

Python – Mapping Matrix with Dictionary

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

When working with matrices (lists of lists) in Python, you often need to map numerical values to meaningful strings using a dictionary. This technique is useful for converting coded data into readable format. Basic Matrix Mapping Here's how to map matrix elements using a dictionary with nested loops − my_matrix = [[2, 4, 3], [4, 1, 3], [2, 1, 3, 4]] print("Original matrix:") print(my_matrix) # Mapping dictionary map_dict = {2: "Python", 1: "fun", 3: "to", 4: "learn"} # Map matrix elements result = [] for row in my_matrix: mapped_row ...

Read More
Showing 3451–3460 of 25,466 articles
« Prev 1 344 345 346 347 348 2547 Next »
Advertisements