Group Strings by K Length Using Suffix in Python

AmitDiwan
Updated on 20-Sep-2021 09:01:30

257 Views

When it is required to group strings by K length using a suffix, a simple iteration and the ‘try’ and ‘except’ blocks are used.ExampleBelow is a demonstration of the samemy_list = ['peek', "leak", 'creek', "weak", "good", 'week', "wood", "sneek"] print("The list is :") print(my_list) K = 3 print("The value of K is ") print(K) my_result = {} for element in my_list: suff = element[-K : ] try: my_result[suff].append(element) except: my_result[suff] = [element] print("The ... Read More

Replace Elements in a List with Greatest Neighbors in Python

AmitDiwan
Updated on 20-Sep-2021 09:00:08

199 Views

When it is required to replace the elements of the list by greatest neighbours, a simple iteration along with the ‘if’ and ‘else’ condition is used.ExampleBelow is a demonstration of the samemy_list = [41, 25, 24, 45, 86, 37, 18, 99] print("The list is :") print(my_list) for index in range(1, len(my_list) - 1): my_list[index] = my_list[index - 1] if my_list[index - 1] > my_list[index + 1] else my_list[index + 1] print("The resultant list is :") print(my_list)OutputThe list is : [41, 25, 24, 45, 86, 37, 18, 99] The resultant list is : [41, ... Read More

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

Filter Dictionaries with Ordered Values in Python

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

247 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

Replace Value by Kth Index Value in Dictionary List in Python

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

619 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

Summation of Consecutive Elements Power in Python

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

181 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

Create MultiIndex from Arrays in Python Pandas

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

372 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

Find Group Sum Till Each K in a List using Python

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

189 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

Find Non-K Distant Elements in Python

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

145 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

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