Convert All Items in a Python List to Lower Case

Here are 2 ways to convert all items in a list to a lower case: (1) Using a List Comprehension: Copy my_list = [“AA”, “BB”, “CC”, “DD”, “EE”]lower_case_list = [i.lower() for i in my_list]print(lower_case_list) The result: (2) Using a For Loop: Copy my_list = [“AA”, “BB”, “CC”, “DD”, “EE”] lower_case_list = [] for i in … Read more

How to Convert String to Pandas DataFrame

Here are 3 ways to convert a string to Pandas DataFrame based on how the string looks like: (1) Multi-line string that contains the column names Copy import pandas as pd from io import StringIO my_string = “””col_1,col_2,col_3 11,22,33 xx,yy,zz 4,5,6 “”” data = StringIO(my_string) df = pd.read_csv(data, sep=”,”) print(df) The string will be converted … Read more

Convert DataFrame Column Values to Absolute Values

To convert DataFrame column values to absolute values: Copy df[“column_name”] = df[“column_name”].abs() The Example To start, create a DataFrame with a column that contains negative values. Here, the “price” column contains two negative values: Copy import pandas as pddata = { “product_name”: [“laptop”, “printer”, “tablet”, “desk”, “chair”], “price”: [1200, -200, -400, 300, 150],}df = pd.DataFrame(data)print(df) … Read more

Convert Pandas DataFrame to HTML Table – Python

To convert Pandas DataFrame to HTML table: Copy html_table = df.to_html() print(html_table) To justify the column labels to the left: Copy html_table = df.to_html(justify=’left’) print(html_table) To remove the index labels and justify the column labels to the left: Copy html_table = df.to_html(index=False, justify=’left’) print(html_table) Example of Converting Pandas DataFrame to HTML Table First, create a … Read more

Convert a List to a Tuple in Python

Here are 3 ways to convert a list to a tuple in Python: (1) Using the tuple() function: Copy my_list = [“item_1”, “item_2”, “item_3”, …]my_tuple = tuple(my_list) (2) Using tuple(i for i in my_list): Copy my_list = [“item_1”, “item_2”, “item_3”, …]my_tuple = tuple(i for i in my_list) (3) Using (*my_list, ): Copy my_list = [“item_1”, … Read more

Convert Tuple to List in Python

Here are 3 ways to convert a tuple to a list in Python: (1) Using a list() function: Copy my_tuple = (“item_1”, “item_2”, “item_3”, …)my_list = list(my_tuple) (2) Using a list comprehension: Copy my_tuple = (“item_1”, “item_2”, “item_3”, …)my_list = [i for i in my_tuple] (3) Using a for loop: Copy my_tuple = (“item_1”, “item_2”, … Read more

Replace All Instances of Characters in a String – Python

To replace all instances of characters in a string in Python: Copy final_string = original_string.replace(“original_value”, “new_value”) And if you want to replace n number of instances: Copy final_string = original_string.replace(“original_value”, “new_value”, n) Examples of Replacing Instances of Characters in a String in Python Example 1: Simple replacement of instances in a string Assume that you … Read more

Convert String to List in Python

To convert a string to a list in Python: Copy my_list = my_string.split(“separated_by”) For example, if your string is separated by spaces, then apply this syntax to convert the string with the spaces into a list (notice that a space was applied inside the brackets of the “split”): Copy my_string = “aa bb cc dd … Read more

Convert List to String in Python

Here are different ways to convert a list to a string in Python. Note that if your list contains numeric data, you’ll first need to convert each element in the list to a string (as captured in points 3 and 4 below). (1) Convert a list that contains text to a string (with spaces among … Read more