AmitDiwan has Published 10744 Articles

Python Pandas - Create Multiindex from arrays

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:53:23

367 Views

We will see how to create multiindex from arrays using the MultiIndex.from_arrays(). At first, let us create an array of cars −car = ['Audi', 'Lexus', 'Tesla', 'Mercedes', 'BMW', 'Toyota', 'Nissan', 'Bentley', 'Mustang']Create another array for our example, that would include the Registration Price −reg_price = [1000, 1400, 1100, 900, 1700, ... Read More

Python program to find the group sum till each K in a list

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:53:01

182 Views

When it is required to find the group sum till each K in a list, a simple iteration and the ‘append’ method are used.ExampleBelow is a demonstration of the samefrom collections import defaultdict my_list = [21, 4, 37, 46, 7, 56, 7, 69, 2, 86, 1] print("The list ... Read More

Python program to find Non-K distant elements

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:38:54

137 Views

When it is required to find non ‘K’ distant elements, a simple iteration along with the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [91, 13, 19, 25, 35, 3, 9, 11, 0] print("The list is :") print(my_list) my_key = 2 print("The key is ") ... Read More

Python - How to Group Pandas DataFrame by Minutes?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:28:03

3K+ Views

We will group Pandas DataFrame using the groupby(). Select the column to be used using the grouper function. We will group minute-wise and calculate the sum of Registration Price with minutes interval for our example shown below for Car Sale Records.At first, let’s say the following is our Pandas DataFrame ... Read More

Python – Find Product of Index Value and find the Summation

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:24:34

145 Views

When it is required to find the product of the index value and the summation, the ‘enumerate’ attribute is used.ExampleBelow is a demonstration of the samemy_list = [71, 23, 53, 94, 85, 26, 0, 8] print("The list is :") print(my_list) my_result = 0 for index, element in ... Read More

Python – Sort Matrix by K Sized Subarray Maximum Sum

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:21:52

217 Views

When it is required to sort matrix by k sized subarray maximum sum, a method is defined that uses the ‘amx’ and ‘sum’ methods and iterates over the list.ExampleBelow is a demonstration of the samedef sort_marix_K(my_list): return max(sum(my_list[index: index + K]) for index in range(len(my_list) - K)) ... Read More

Python – Extract element from a list succeeded by K

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:17:43

215 Views

When it is required to extract element from a list succeeded by ‘K’, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [45, 65, 32, 78, 99, 10, 21, 2] print("The list is : ") print(my_list) K = 99 print("The value ... Read More

Python - Filter Pandas DataFrame by Time

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:16:43

650 Views

To filter DataFrame by time, use the loc and set the condition in it to fetch records. At first, import the required library −import pandas as pdCreate a Dictionary of list with date records −d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_Purchase': ['2021-07-10', '2021-08-12', '2021-06-17', '2021-03-16', '2021-05-19', '2021-08-22'] ... Read More

Python – Test if list is Palindrome

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:16:18

2K+ Views

When it is required to test if a list is a palindrome, a method is defined that reverses the string and checks if it is equal to the original string. Based on the result, relevant message is displayed on the console. A list comprehension and the ‘join’ method are used.ExampleBelow ... Read More

Python – Row with Minimum difference in extreme values

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:14:10

66 Views

When it is required to get the row with minimum difference in extreme values, list comprehension, the ‘min’ method and ‘max’ methods are used.ExampleBelow is a demonstration of the samemy_list = [[41, 1, 38], [25, 33, 1], [13, 44, 65], [1, 22]] print("The list is : ") print(my_list) ... Read More

Advertisements