How to Transpose a DataFrame in R

To transpose a DataFarme in R using the t() function: Copy transposed_df <- t(df) Example of Transposing a DataFrame in R Given the following DataFrame in R: Copy df <- data.frame(product = c(“computer”, “monitor”, “keyboard”, “printer”, “tablet”), price = c(800, 450, 100, 150, 300), brand = c(“A”, “B”, “C”, “X”, “Y”) ) print(df) The current … Read more

Create Parametrize Pytest Test for a Function in Python

In this guide, you’ll see how to create a parametrize Pytest test for a given function in Python. To start, here is one simple template to create a parametrize Pytest test. In the next section, you’ll see how to apply this template using a practical example. Copy import pytest # Import the function you want … Read more

Change the Version of PIP on Windows

The following command can be used to change the version of PIP on Windows: Copy python -m pip install pip==pip_version_needed Note that if you already added Python to Windows Path, then simply open the Windows Command Prompt and type the above command. Otherwise, check the full steps below. Steps to Change the Version of PIP … Read more

Use Poetry to Install Python Packages

In this short guide, you’ll see the full steps to use Poetry to install Python packages and manage dependencies. Steps to use Poetry Step 1: Create a new project To start, create a new project (or navigate to an existing one). For example, let’s say that you created a project called “check_poetry“ Step 2: Install … Read more

Create Executable of Python Script using cx_Freeze

In this short guide, you’ll see how to create an executable of a Python script using cx_Freeze. Here are the full steps to create an executable on Windows. Steps to Create an Executable using cx_Freeze Step 1: Add Python to Windows Path If you haven’t already done so, add Python to Windows path. You can … Read more

Check the PIP Version on Windows

In this short guide, you’ll see how to check the PIP version on Windows. The general command to check the PIP version on Windows is: Copy pip –version Note that if you already added Python to Windows Path, then simply open the Command Prompt and type the above command. Otherwise, check the full steps below. … Read more

Count the Number of Keys in a Dictionary in Python

To count the number of keys in a dictionary in Python: Copy my_dict = {“key_1”: 1, “key_2”: 2, “key_3”: 3, “key_4”: 4, “key_5”: 5} count_keys = len(my_dict) print(count_keys) Here, the result is 5 keys. Count the number of keys in a nested dictionary The following function can be used to count the number of keys … Read more

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