How to Convert String to Dictionary in Python

Here are 3 ways to convert a string to a dictionary in Python: (1) ast.literal_eval(my_string) to convert a string that looks like a dictionary to an actual dictionary: Copy import ast my_string = ‘{1: “blue”, 2: “green”, 3: “red”, 4: “yellow”, 5: “purple”}’ my_dict = ast.literal_eval(my_string) print(my_dict) The result is a dictionary: Examples of different … Read more

Capitalize the First Letter of each Element in a Python List

Two approaches to capitalize the first letter of each element in a Python list: (1) Using a List Comprehension: Copy my_list = [“red color”, “blue color”, “green color”]capitalize_list = [i.capitalize() for i in my_list]print(capitalize_list) As highlighted in yellow, the first letter of each element in the list is now capitalized: Please check the following guide … Read more

Apply a Title Case to each Element in a Python List

Here are 3 ways to apply a title case to each element in a Python list (special case for apostrophes is included): (1) Using a List Comprehension: Copy my_list = [“red color”, “blue color”, “green color”]title_list = [i.title() for i in my_list]print(title_list) As highlighted in yellow, a title case was applied to each element in … Read more

Convert all Elements in a Python List to Upper Case

Here are 2 ways to convert all elements in a list to upper case: (1) Using a List Comprehension: Copy my_list = [“aa”, “bb”, “cc”, “dd”, “ee”]upper_case_list = [i.upper() for i in my_list]print(upper_case_list) The result: (2) Using a For Loop: Copy my_list = [“aa”, “bb”, “cc”, “dd”, “ee”]upper_case_list = []for i in my_list: upper_case_list.append(i.upper())print(upper_case_list) The … Read more

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