
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

1K+ Views
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

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

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

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

718 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

662 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

351 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

509 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

244 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

195 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