Python
- 23 December
Remove 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 December
How 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. [crayon-678e2dd3c07a5811836889/] [crayon-678e2dd3c07a9828593250/] A backslash (\) is an explicit line break operator in […]
- 21 December
Save 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 December
Write 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. [crayon-678e2dd3c0a7a708645792/] [crayon-678e2dd3c0a7d766160244/] We created a byte_list using a […]
- 16 December
Convert 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. [crayon-678e2dd3c0bf0227248771/] [crayon-678e2dd3c0bf4368264598/] First, we created a list having string-type elements and named that list list_of_strings. Next, we used the […]
- 09 December
Count 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. [crayon-678e2dd3c11da701086588/] We will get the following result after running the above program. [crayon-678e2dd3c11f6365834900/] The count() is a conventional method in Python. It holds a substring, start, […]
- 08 December
Find 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. [crayon-678e2dd3c17de450427835/] […]
- 07 December
Get 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. [crayon-678e2dd3c1d3c556744514/] The execution […]
- 05 December
Remove 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, "") [crayon-678e2dd3c21d5309091927/] The code above will print the following output on the console: [crayon-678e2dd3c21d9463599722/] We tried removing the substring from my_string. […]