Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1062 of 3366
373 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, 1800, 1300, 1150, 1350]Now, we will use the MultiIndex.from_arrays(). Also set the names for the levels in the index.ExampleFollowing is the code −import pandas as pd # array of cars car = ['Audi', 'Lexus', 'Tesla', 'Mercedes', 'BMW', 'Toyota', 'Nissan', 'Bentley', 'Mustang'] # array of registration price reg_price = ... Read More
146 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 ") print(my_key) my_result = [] for element in my_list: if element + my_key not in my_list and element - my_key not in my_list: my_result.append(element) print("The resultant list is :") print(my_result)OutputThe list is : [91, 13, 19, 25, 35, 3, 9, 11, ... Read More
159 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 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
232 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)) 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
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 with three columns. We have set Date_of_Purchase with timestamp, including Date and Time both −dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"], "Date_of_Purchase": [ pd.Timestamp("2021-07-28 00:10:00"), pd.Timestamp("2021-07-28 00:12:00"), ... Read More
223 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 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
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 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
78 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) 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
667 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'] }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
361 Views
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