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 Based on Conditions in a DataFrame in R

Here are several ways to filter rows based on conditions in a DataFrame in R: (1) Filter rows based on a single condition: To maintain only the rows where the price is greater than 400: Copy df <- data.frame(product = c(“laptop”, “printer”, “tablet”, “desk”, “chair”), brand = c(“A”, “B”, “X”, “Y”, “Z”), price = c(1200, … 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

How to Merge two DataFrames in R

Here are the different ways to merge two DataFrames in R: (1) Inner Join: Copy inner_merged <- merge(df1, df2, by = “Id”) (2) Left Join: Copy left_merged <- merge(df1, df2, by = “Id”, all.x = TRUE) (3) Right Join: Copy right_merged <- merge(df1, df2, by = “Id”, all.y = TRUE) (4) Full Outer Join: Copy … Read more

Remove a Column in a DataFrame in R

Here are 3 ways to remove a single column in a DataFrame in R: Using subset() Copy df <- subset(df, select = -column_name_to_remove) Using the indexing operator [] Copy df <- df[, -which(names(df) == “column_name_to_remove”)] Using the column index: Copy df <- subset(df, select = -column_index_to_remove) Here are additional 3 ways to remove multiple columns … Read more

Rename Columns in a DataFrame in R

Here are several ways to rename columns in a DataFrame in R: (1) Use the colnames() function to rename column/s in a DataFrame in R: By specifying the column name to rename a single column: Copy colnames(df)[colnames(df) == “old_column_name”] <- “new_column_name” By specifying the column index to rename a single column: Copy colnames(df)[column_index] <- “new_column_name” … Read more