Found 33676 Articles for Programming

Python program to find Least Frequent Character in a String

AmitDiwan
Updated on 20-Sep-2021 10:20:05

720 Views

When it is required to find the least frequent character in a string, ‘Counter’ is used to get the count of letters. The ‘min’ method is used to get the minimum of values in the string, i.e every letter’s count is stored along with the letter. The minimum is obtained.ExampleBelow is a demonstration of the samefrom collections import Counter my_str = "highland how" print ("The string is : ") print(my_str) my_result = Counter(my_str) my_result = min(my_result, key = my_result.get) print ("The minimum of all characters in the string is : ") print(my_result)OutputThe string is : highland ... Read More

Python Pandas - Fill NaN with Linear Interpolation

AmitDiwan
Updated on 20-Sep-2021 09:25:51

663 Views

To fill NaN with Linear Interpolation, use the interpolate() method on the Pandas series. At first, import the required libraries −import pandas as pd import numpy as npCreate a Pandas series with some NaN values. We have set the NaN using the numpy np.nan −d = pd.Series([10, 20, np.nan, 40, 50, np.nan, 70, np.nan, 90, 100]) Find linear interpolation −d.interpolate()ExampleFollowing is the code −import pandas as pd import numpy as np # pandas series d = pd.Series([10, 20, np.nan, 40, 50, np.nan, 70, np.nan, 90, 100]) print"Series...", d # interpolate print"Linear Interpolation...", d.interpolate()OutputThis will produce the following ... Read More

Python - How to group DataFrame rows into list in Pandas?

AmitDiwan
Updated on 20-Sep-2021 09:19:45

352 Views

To group dataframe rows into list, use the apply() function. At first, let us import the require library −import pandas as pdCreate DataFrame with 2 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Grouping DataFrame into list with apply(list) −dataFrame = dataFrame.groupby('Car')['Units'].apply(list) ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": ... Read More

Python - Merge Pandas DataFrame with Right Outer Join

AmitDiwan
Updated on 20-Sep-2021 09:10:56

510 Views

To merge Pandas DataFrame, use the merge() function. The right outer join is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “right”At first, let us import the pandas library with an alias −import pandas as pd Create two dataframes to be merged −# Create DataFrame1 dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90]    } ) # Create DataFrame2 dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": ... Read More

Python Program to Group Strings by K length Using Suffix

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

248 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

Python – Replacing by Greatest Neighbors in a List

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

196 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

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

612 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

178 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

Advertisements