AmitDiwan has Published 10744 Articles

Python – Remove Rows for similar Kth column element

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:04:30

118 Views

When it is required to remove rows for a similar ‘K’th column element, a simple iteration, and the ‘append’ method are used.ExampleBelow is a demonstration of the same −my_list = [[45, 95, 26], [70, 35, 74], [87, 65, 23], [70, 35, 74], [67, 85, 12], [45, 65, 0]] print("The ... Read More

Python – Count frequency of sublist in given list

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:01:44

457 Views

When it is required to count the frequency of a sub-list in a given list, a list comprehension and the ‘len’ method along with the ‘if’ condition are used.ExampleBelow is a demonstration of the same −my_list = [23, 33, 45, 67, 54 , 43, 33, 45, 67, 83, 33, 45, ... Read More

Python – Segregate elements by delimiter

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 10:59:06

189 Views

When it is required to segregate elements based on a delimiter, ExampleBelow is a demonstration of the same −my_list = ["89@21", "58@51", "19@61", "11@10", "32@65", "34@45", "87@90", "32@21", '1@345'] print("The list is : " ) print(my_list) print("The list after sorting is :") my_list.sort() print(my_list) my_delimiter = "@" print("The ... Read More

Python – Extract elements in between multiple specific range of index

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 10:48:40

717 Views

When it is required to extract elements that are in between multiple specific range of index, the ‘extend’ method, a simple iteration and indexing are used.ExampleBelow is a demonstration of the same −my_list = [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0] print("The list is : ... Read More

Python – Sort List items on basis of their Digits

AmitDiwan

AmitDiwan

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

364 Views

When it is required to sort the elements of a list based on the digits, the ‘max’, ‘max’ method are used. We will also use the ‘sorted’ method, the ‘lambda’ function and ‘ljust’.ExampleBelow is a demonstration of the same −my_list = [4344, 2611, 122, 541, 33, 892, 48292, 460, 390, ... Read More

Python – Random insertion of elements K times

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 10:43:09

299 Views

When it is required to randomly insert elements K times, the ‘random’ package and methods from the random package along with a simple iteration is used.ExampleBelow is a demonstration of the same −import random my_list = [34, 12, 21, 56, 8, 9, 0, 3, 41, 11, 90] print("The ... Read More

Python program to find all the Combinations in a list with the given condition

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 10:40:48

572 Views

When it is required to find all the combinations in a list with a specific condition, a simple iteration, the ‘isinstance’ method, the ‘append’ method and indexing are used.ExampleBelow is a demonstration of the same −print("Method definition begins") def merge_the_vals(my_list_1, my_list_2, K): index_1 = 0 ... Read More

Python – Drop multiple levels from a multi-level column index in Pandas dataframe

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 09:15:08

6K+ Views

To drop multiple levels from a multi-level column index, use the columns.droplevel() repeatedly. We have used the Multiindex.from_tuples() is used to create indexes column-wise.At first, create indexes column-wise −items = pd.MultiIndex.from_tuples([("Col 1", "Col 1", "Col 1"), ("Col 2", "Col 2", "Col 2"), ("Col 3", "Col 3", "Col 3")])Next, create a ... Read More

Python Program – Convert String to matrix having K characters per row

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 08:19:15

742 Views

When it is required to convert a string into a matrix that has ‘K’ characters per row, a method is defined that takes a string and a value for ‘K’. It uses a simple iteration, the modulus operator and the ‘append’ method.ExampleBelow is a demonstration of the same −print("Method definition ... Read More

Python – To convert a list of strings with a delimiter to a list of tuple

AmitDiwan

AmitDiwan

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

328 Views

When it is required to convert a list of strings with a delimiter to a list of tuples, a K value is set, and list comprehension along with the ‘split’ method is used.ExampleBelow is a demonstration of the same −my_list = ["33-22", "13-44-81-39", "42-10-42", "36-56-90", "34-77-91"] print("The list is ... Read More

Advertisements