Python
23 DecemberRemove All Non-numeric Characters in Pandas
1. Introduction One of the common tasks that pandas users face is to remove all non-numeric characters from a column or a dataframe. Non-numeric characters are any characters that are not digits, such as letters, punctuation, symbols, or whitespace. Removing non-numeric characters can help to clean the data and prepare it for further processing or […]
23 DecemberHow To Do Line Continuation in Python
Using Backslash (\) Operator We can use \ operator for line continuation in string and number expression as follows. Line Continuation in String To do line continuation in Python Strings: Use the backslash operator to do line continuation for writing strings in Python. #use backslash in a string with single quotation marks my_string1 = 'This […]
21 DecemberSave Object to File in Python
1. Introduction In Python, one often encounters the need to save complex objects, like instances of a class, to a file for purposes such as data persistence. Let’s take a Student class as an example, which includes attributes like name, email, age, city, courses, and address. The goal is to efficiently serialize this object into […]
18 DecemberWrite Binary File in Python
Use bytearray() Function To write a binary file in Python: Use the bytearray() function to convert the list of bytes to a bytearray type object. Use a with clause with open() the method in write binary mode(wb) Use write() method to write the byte_array to the file. byte_list = [100, 56, 35, 94] byte_array = […]
16 DecemberConvert List to Comma Separated String in Python
Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. list_of_strings = ['string_one', 'string_two', 'string_three'] comma_separated_strings = ','.join(list_of_strings) print(comma_separated_strings) string_one,string_two,string_three First, we created a list having string-type elements and […]
09 DecemberCount Occurrences of Character in String in Python
Using count() Method Use String’s count() method to count occurrences of character in String in Python e.g. my_string.count(character). count() method returns number of occurrences of the character in String. my_string = "This code snippet counts occurrences of a character in a string in python" character = 'c' count = my_string.count(character) print(f'Occurrences of character \'{character}\': {count}') […]
08 DecemberFind Character in String in Python
Using find() Method To find the character in a string in Python: Use the find() method to find the index of the first occurrence of the supplied character in the input String. Use an if statement to check if the returned index is not -1; if so, print that index; otherwise, print an error. my_string […]
07 DecemberGet List of Months Between Two Dates in Python
Use pandas.period_range() Method To get the list of months between two specified dates in Python: Use pandas’s period_range() to get list of months between start date and end date with freq='M'. Use list comprehension to iterate over list of months. For each month, use strftime() method to convert it in appropriate format. import pandas as […]
05 DecemberRemove Substring from String in Python
Use replace() Method To eliminate a substring from string in Python: Define and initiate my_string. Declare substring to remove it from my_string. Use replace() method to remove substring from string. e.g. my_string = my_string.replace(substring, "") my_string = "The code $#jc@ snippet removes a substring $#jc@ from a string in python." substring = "$#jc@ " print(f'My […]