• 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-66050aec33fe4364193727/] [crayon-66050aec33fef205314873/] 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-66050aec35de0844448316/] [crayon-66050aec35de7992877126/] 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-66050aec35f9e877583792/] [crayon-66050aec35fa2540344321/] 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-66050aec360d9018761138/] We will get the following result after running the above program. [crayon-66050aec360dd285062088/] 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-66050aec3ad8d621160274/] […]

  • 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-66050aec3ccc7631725746/] 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-66050aec40b3d116439911/] The code above will print the following output on the console: [crayon-66050aec40b46012835348/] We tried removing the substring from my_string. […]