To fill NaN with Polynomial Interpolation, use the interpolate() method on the Pandas series. With that, set the “method” parameter to “polynomial”.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, 65, 75, 85, np.nan, 100]) Find polynomial interpolation using the method parameter of the interpolate() method −d.interpolate(method='polynomial', order=2)ExampleFollowing is the code −import pandas as pd import numpy as np # pandas series d = pd.Series([10, 20, np.nan, 65, 75, 85, np.nan, 100]) ... Read More
When it is required to find words that are greater than a specific length, a method is defined that splits the string, and iterates through it. It checks the length of the word and compares it with the given length. If they match, it is returned as output.ExampleBelow is a demonstration of the samedef string_check(string_length, my_string): result_string = [] words = my_string.split(" ") for x in words: if len(x) > string_length: result_string.append(x) return result_string string_length = 3 my_string ="Python is always fun to learn" print("The string is :") ... Read More
When it is required to check if a given string contains specific characters using regular expression, a regular expression pattern is defined, and the string is subjected to follow this pattern.ExampleBelow is a demonstration of the sameimport re def check_string(my_string, regex_pattern): if re.search(regex_pattern, my_string): print("The string contains the defined characters only") else: print("The doesnot string contain the defined characters") regex_pattern = re.compile('^[Python]+$') my_string_1 = 'Python' print("The string is :") print(my_string_1) check_string(my_string_1 , regex_pattern) my_string_2 = 'PythonInterpreter' print("The string ... Read More
When it is required to check if a string ends with an alphanumeric character or not, the regular expression is used. A method is defined that checks to see an alphanumeric characters, and returns the string as output.ExampleBelow is a demonstration of the sameimport re regex_expression = '[a-zA-z0-9]$' def check_string(my_string): if(re.search(regex_expression, my_string)): print("The string ends with alphanumeric character") else: print("The string doesnot end with alphanumeric character") my_string_1 = "Python@" print("The string is :") print(my_string_1) check_string(my_string_1) ... Read More
When it is required to check if a string begins and ends with the same character or not, regular expression can be used. A method can be defined that uses the ‘search’ function to see if a string begins and ends with a specific character.ExampleBelow is a demonstration of the sameimport re regex_expression = r'^[a-z]$|^([a-z]).*\1$' def check_string(my_string): if(re.search(regex_expression, my_string)): print("The given string starts and ends with the same character") else: print("The given string doesnot start and end with the ... Read More
Rug plots are used to visualize the distribution of data. It is a plot of data for a single variable, displayed as marks along an axis. To make a rug plot in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x data points using numpy.Add representation of a kernel-density estimate using Gaussian kernels, kde1 and kde2.Create a new figure or activate an existing figure using figure() method.Add an 'ax1' to the figure as part of a subplot arrangement.Make a rug plot with marker_size=20.Plot x_eval, kde1(x_eval) and kde2(x_eval) data ... Read More
When it is required to check if a string starts with a specific substring or not, with the help of regular expression, a method is defined that iterates through the string and uses the ‘search’ method to check if a string begins with a specific substring or not.ExampleBelow is a demonstration of the sameimport re def check_string(my_string, sub_string) : if (sub_string in my_string): concat_string = "^" + sub_string result = re.search(concat_string, my_string) if result : ... Read More
When it is required to find sequences of an upper case letter followed by lower case using regular expression, a method named ‘match_string’ is defined that uses the ‘search’ method to match a regular expression. Outside the method, the string is defined, and the method is called on it by passing the string.ExampleBelow is a demonstration of the sameimport re def match_string(my_string): pattern = '[A-Z]+[a-z]+$' if re.search(pattern, my_string): return('The string meets the required condition ') else: return('The string doesnot meet the required condition ') print("The string is ... Read More
When it is required to remove all characters except for letters and numbers, regular expressions are used. A regular expression is defined, and the string is subjected to follow this expression.ExampleBelow is a demonstration of the sameimport re my_string = "python123:, .@! abc" print ("The string is : ") print(my_string) result = re.sub('[\W_]+', '', my_string) print ("The expected string is :") print(result)OutputThe string is : python123:, .@! abc The expected string is : python123abcExplanationThe required packages are imported.A string is defined and is displayed on the console.A regular expression is defined, and the string is subjected ... Read More
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP