AmitDiwan has Published 10744 Articles

Python – Descending Order Sort grouped Pandas dataframe by group size?

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 08:03:05

4K+ Views

To group Pandas dataframe, we use groupby(). To sort grouped dataframe in descending order, use sort_values(). The size() method is used to get the dataframe size.For descending order sort, use the following in sort_values() −ascending=FalseAt first, create a pandas dataframe −dataFrame = pd.DataFrame( { ... Read More

Python – Sort given list of strings by part the numeric part of string

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:49:18

979 Views

When it is required to sort a given list of strings based on a numeric part of the string, a method is defined that uses the regular expressions, the ‘map’ method and the ‘list’ method to display the result.ExampleBelow is a demonstration of the same −import re print("The regular expression ... Read More

Python – Test for desired String Lengths

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:45:53

176 Views

When it is required to test for desired string lengths, a simple iteration, and the ‘len’ method is used.ExampleBelow is a demonstration of the same −my_list = ["python", 'is', 'fun', 'to', 'learn', 'Will', 'how'] print("The list is :") print(my_list) length_list = [6, 2, 3, 2, 5, 4, 3] ... Read More

Python program to Sort a List of Strings by the Number of Unique Characters

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:45:23

300 Views

When it is required to sort a list of strings based on the number of unique characters, a method is defined that uses a ‘set’ operator, the ‘list’ method and the ‘len’ method.ExampleBelow is a demonstration of the same −def my_sort_func(my_elem): return len(list(set(my_elem))) my_list = ['python', ... Read More

Python – Substitute prefix part of List

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:39:39

190 Views

When it is required to substitute prefix part of a list, the ‘len’ method and the ‘:’ operator is used.ExampleBelow is a demonstration of the same −my_list_1 = [15, 44, 82] my_list_2 = [29, 77, 19, 44, 26, 18] print("The first list is : " ) print(my_list_1) print("The ... Read More

Python – Cross mapping of Two dictionary value lists

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:35:34

635 Views

When it is required to cross-map two dictionary valued lists, the ‘setdefault’ and ‘extend’ methods are used.ExampleBelow is a demonstration of the same −my_dict_1 = {"Python" : [4, 7], "Fun" : [8, 6]} my_dict_2 = {6 : [5, 7], 8 : [3, 6], 7 : [9, 8]} print("The first ... Read More

Python – Check if elements index are equal for list elements

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:31:34

628 Views

When it is required to check if the index of the elements is equal to the elements in the list, a simple iteration and the enumerate attribute is used.ExampleBelow is a demonstration of the same −my_list_1 = [12, 62, 19, 79, 58, 0, 99] my_list_2 = [12, 74, 19, 54, ... Read More

Python – Convert List to Index and Value dictionary

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:28:01

683 Views

When it is required to convert a list into an index value dictionary, the ‘enumerate' and a simple iteration are used.ExampleBelow is a demonstration of the same −my_list = [32, 0, 11, 99, 223, 51, 67, 28, 12, 94, 89] print("The list is :") print(my_list) my_list.sort(reverse=True) print("The sorted ... Read More

Python – Extend consecutive tuples

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:24:47

143 Views

When it is required to extend consecutive tuples, a simple iteration is used.ExampleBelow is a demonstration of the same −my_list = [(13, 526, 73), (23, 67, 0, 72, 24, 13), (94, 42), (11, 62, 23, 12), (93, ), (83, 61)] print("The list is :") print(my_list) my_list.sort(reverse=True) print("The list ... Read More

Python – Filter unique valued tuples

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:23:26

244 Views

When it is required to filter unique valued tuples from a list of tuples, the ‘list’ and ‘set’ methods are used.ExampleBelow is a demonstration of the same −my_list = [(42, 51), (46, 71), (14, 25), (26, 91), (56, 0), (11, 1), (99, 102)] print("The list of tuple is :") print(my_list) ... Read More

Advertisements