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 enumerate(my_list): my_result += (index + 1) * element print("The resultant sum is :") print(my_result)OutputThe list is : [71, 23, 53, 94, 85, 26, 0, 8] The resultant sum is : 1297ExplanationA list of integers is defined and is displayed on the console.An integer value is assigned to 0.The ... Read More
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)) my_list = [[51, 23, 4, 24, 1], [45, 6, 26, 36, 5], [56, 16, 6, 36, 8], [5, 4, 36, 26, 26]] print("The list is :") print(my_list) K = 4 print("The value of K is ") print(K) my_list.sort(key=sort_marix_K) print("The resultant list is :") print(my_list)OutputThe list is ... Read More
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 of K is ") print(K) my_result = [] for elem in range(len(my_list) - 1): if my_list[elem + 1] == K: my_result.append(my_list[elem]) print("The result is : " ) print(my_result)OutputThe list is : [45, 65, 32, 78, 99, 10, 21, ... Read More
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'] }Creating a dataframe from the above dictionary of lists −dataFrame = pd.DataFrame(d) Now, let’s say we need to fetch cars purchased after a specific date. For this, we use loc −resDF = dataFrame.loc[dataFrame["Date_of_Purchase"] > "2021-07-15"]ExampleFollowing is the complete code −import pandas as pd # dictionary of ... Read More
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 is a demonstration of the samedef check_palindrome_list(my_str): if my_str == my_str[::-1]: print("The list is a palindrome") else: print("The list isn't a palindrome") my_list = [77, 1, 56, 65, 1, 77] ... Read More
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) my_min_val = min([max(elem) - min(elem) for elem in my_list]) my_result = [elem for elem in my_list if max(elem) - min(elem) == my_min_val] print("The result is : ") print(my_result)OutputThe list is : [[41, 1, 38], [25, 33, 1], [13, 44, 65], [1, 22]] The result is : [[1, 22]]ExplanationA ... Read More
When it is required to remove palindromic elements from a list, list comprehension and the ‘not’ operator are used.ExampleBelow is a demonstration of the samemy_list = [56, 78, 12, 32, 4, 8, 9, 100, 11] print("The list is : ") print(my_list) my_result = [elem for elem in my_list if int(str(elem)[::-1]) not in my_list] print("The result is : " ) print(my_result)OutputThe list is : [56, 78, 12, 32, 4, 8, 9, 100, 11] The result is : [56, 78, 12, 32, 100]ExplanationA list is defined and displayed on the console.A list comprehension is used to iterate over the ... Read More
When it is required to split ‘N’ sized substrings with ‘K’ distinct characters, it is iterated over, and the ‘set’ method is used to get the different combinations.ExampleBelow is a demonstration of the samemy_string = 'Pythonisfun' print("The string is : ") print(my_string) my_substring = 2 my_chars = 2 my_result = [] for idx in range(0, len(my_string) - my_substring + 1): if (len(set(my_string[idx: idx + my_substring])) == my_chars): my_result.append(my_string[idx: idx + my_substring]) print("The resultant string is : ") print(my_result)OutputThe string is : Pythonisfun The resultant string is : ['Py', 'yt', ... Read More
Use the group.size() to count the number of rows in each group. Import the required library −import pandas as pdCreate a DataFrame −dataFrame = pd.DataFrame({'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, 25, 50], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'] })Group by columns −dataFrame.groupby(["Product Category", "Quantity"]) Now, count the group size to get the count of rows in each group.ExampleFollowing is the complete code −import pandas as pd # create a dataframe dataFrame = pd.DataFrame({'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, ... Read More
To sort data in ascending or descending order, use sort_values() method. For descending order, use the following in the sort_values() method −ascending=FalseImport the required library −import pandas as pd Create a DataFrame with 3 columns −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 2000], "Place": ['Pune', 'Delhi', 'Mumbai', 'Hyderabad', 'Bangalore', 'Chandigarh'] } )To sort DataFrame in descending order according to the element frequency, we need to count the occurrences. Therefore, count() is also used with sort_values() set for descending order sort −dataFrame.groupby(['Car'])['Reg_Price'].count().reset_index(name='Count').sort_values(['Count'], ascending=False)ExampleFollowing is ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP