
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
Found 10476 Articles for Python

237 Views
When it is required to filter dictionaries with ordered values, the ‘sorted’ method along with the list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [{'python': 2, 'is': 8, 'fun': 10}, {'python': 1, 'for': 10, 'coding': 9}, {'cool': 3, 'python': 4}] print("The list is :") print(my_list) my_result = [index for index in my_list if sorted( list(index.values())) == list(index.values())] print("The resultant dictionary is :") print(my_result)OutputThe list is : [{'python': 2, 'fun': 10, 'is': 8}, {'python': 1, 'coding': 9, 'for': 10}, {'python': 4, 'cool': 3}] The resultant dictionary ... Read More

1K+ Views
To filter DataFrame between two dates, use the dataframe.loc. At first, import the required library −import pandas as pdCreate a Dictionary of lists 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-02-19', '2021-08-22'] }Creating dataframe from the above dictionary of listsdataFrame = pd.DataFrame(d) Fetch car purchased between two dates i.e. 1st Date: 2021-05-10 and 2nd Date: 2021-08-25 −resDF = dataFrame.loc[(dataFrame["Date_of_Purchase"] >= "2021-05-10") & (dataFrame["Date_of_Purchase"] = "2021-05-10") & (dataFrame["Date_of_Purchase"] Read More

611 Views
When it is required to replace the value by Kth index value in a list of dictionary, the ‘isinstance’ method and a simple iteration are used.ExampleBelow is a demonstration of the samemy_list = [{'python': [5, 7, 9, 1], 'is': 8, 'good': 10}, {'python': 1, 'for': 10, 'fun': 9}, {'cool': 3, 'python': [7, 3, 9, 1]}] print("The list is :") print(my_list) K = 2 print("The value of K is") print(K) my_key = "python" for index in my_list: if isinstance(index[my_key], list): index[my_key] = index[my_key][K] print("The result is :") ... Read More

177 Views
When it is required to add the consecutive elements power, an ‘if’ condition and a simple iteration along with the ‘**’ operator are used.ExampleBelow is a demonstration of the samemy_list = [21, 21, 23, 23, 45, 45, 45, 56, 56, 67] print("The list is :") print(my_list) my_freq = 1 my_result = 0 for index in range(0, len(my_list) - 1): if my_list[index] != my_list[index + 1]: my_result = my_result + my_list[index] ** my_freq my_freq = 1 else: my_freq += 1 ... Read More

183 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 is :") print(my_list) my_key = 46 print("The key is ") print(my_key) my_sum = 0 my_result = [] for ele in my_list: if ele != my_key: my_sum += ele else: my_result.append(my_sum) my_result.append(ele) ... Read More

368 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

139 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

148 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

219 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