Found 10476 Articles for Python

Python – Filter dictionaries with ordered values

AmitDiwan
Updated on 20-Sep-2021 08:58:50

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

Python Pandas – Filter DataFrame between two dates

AmitDiwan
Updated on 20-Sep-2021 08:59:47

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

Python – Replace value by Kth index value in Dictionary List

AmitDiwan
Updated on 20-Sep-2021 08:57:29

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

Python – Summation of consecutive elements power

AmitDiwan
Updated on 20-Sep-2021 08:55:36

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

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

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

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

Python Pandas - Create Multiindex from arrays

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

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

Python program to find Non-K distant elements

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

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

Python – Find Product of Index Value and find the Summation

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

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

Python – Sort Matrix by K Sized Subarray Maximum Sum

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

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

Python - How to Group Pandas DataFrame by Minutes?

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 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

Advertisements