Convert All Elements in a List to Upper Case – Python

Here are 2 ways to convert all elements in a list to upper case: (1) Using a List Comprehension: 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: my_list = [‘aa’, ‘bb’, ‘cc’, ‘dd’, ‘ee’] upper_case_list = [] for i in my_list: … 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: 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: 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 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 into … Read more

Convert DataFrame Column Values to Absolute Values

To convert DataFrame column values to absolute values: df[‘column_name’] = df[‘column_name’].abs() Example of Converting DataFrame Column Values to Absolute Values To start, create a DataFrame with a column that contains negative values. Here, the ‘price‘ column contains two negative values: import pandas as pd data = {‘product_name’: [‘laptop’, ‘printer’, ‘tablet’, ‘desk’, ‘chair’], ‘price’: [1200, -200, … Read more

Convert Pandas DataFrame to HTML Table – Python

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

How to Convert a List to a Tuple in Python

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

How to Convert Tuple to List in Python

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

Count the Number of Times an Item Appears in a List – Python

Here are 3 ways to count the number of times an item appears in a list in Python: (1) Using count() to count a specific item. For example, let’s count the number of times ‘mm’ appears in a list: my_list = [‘mm’, ‘bb’, ‘cc’, ‘mm’, ‘mm’, ‘cc’] count_item = my_list.count(‘mm’) print(count_item) The result is 3 … Read more

Replace All Instances of Characters in a String – Python

Here is a simple way to replace all instances of characters in a string in Python: final_string = original_string.replace(‘original_value’, ‘new_value’) And if you want to replace n number of instances (where n is the number of instances needed): final_string = original_string.replace(‘original_value’, ‘new_value’, n) Examples of Replacing Instances of Characters in a String in Python Example … Read more

How to Convert String to List in Python

Here is the general syntax to convert a string to a list in Python: my_list = my_string.split(‘separated_by’) For example, if your string is separated by spaces, then you may apply the following syntax to convert the string with spaces into a list (notice that a space was applied inside the brackets of the “split”): my_string … Read more