Convert Excel File to JSON using Python

To convert Excel file to a JSON file using Python: Copy import pandas as pd # Load Excel to DataFrame path_excel = ‘path_to_excel_file.xlsx’ df = pd.read_excel(path_excel, engine=’openpyxl’) # Convert DataFrame to JSON json_data = df.to_json(orient=’records’, indent=4) print(json_data) # Write JSON data to file path_json = ‘final_result.json’ with open(path_json, ‘w’) as json_file: json_file.write(json_data) In case needed, … Read more

Convert JSON to Excel using Python

To convert JSON data to Excel using Python: Copy # Json to DataFrame df = pd.json_normalize(json_data) # DataFrame to Excel excel_filename = ‘json_data_to_excel.xlsx’ df.to_excel(excel_filename, index=False) In case needed, install the openpyxl package using pip install openpyxl. Example 1 of converting JSON to Excel using Python The Python script: Copy import pandas as pd json_data = … Read more

Filter Rows in Pandas DataFrame using RegEx

Here are different ways to filter rows in Pandas DataFrame using RegEx: (1) Get all the rows where the name starts with ‘B’: Copy import pandas as pd data = {‘name’: [‘Bill’, ‘Maria’, ‘David’, ‘April’, ‘Bob’], ‘age’: [28, 42, 33, 57, 25], ‘country’: [‘Brazil’, ‘Mexico’, ‘Cambodia’, ‘Bolivia’, ‘India’] } df = pd.DataFrame(data) pattern = r’^B\w*’ … Read more

Convert String to Tuple in Python

Here are the different ways to convert a string to a tuple in Python: (1) String that contains integers separated by a delimiter (such as a comma): Copy my_string = “11,22,33,44,55”my_tuple = tuple(my_string.split(“,”))print(my_tuple) The result is a tuple, where each element in the tuple is a string: To convert the elements in the tuple to … Read more

Convert Tuple to String in Python

Here are two ways to convert a tuple to a string in Python. Note that if your tuple contains numeric data (such as integers), you’ll need to apply a different code (point 2 below), where you first convert each element in the tuple to a string. (1) Convert a tuple that contains text to a … Read more

Convert Dictionary to String in Python

Here are 2 ways to convert a dictionary to a string in Python: (1) Using json.dumps() to convert dictionary to string: Copy import json my_dictionary = {1: “blue”, 2: “green”, 3: “red”, 4: “yellow”, 5: “purple”} my_string = json.dumps(my_dictionary) print(my_string) print(type(my_string)) The result is a string: Examples of different variations of dictionaries (inside a list) … Read more

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