Import pandas as pd – Getting Started with Pandas

In this short guide, you’ll see how to get started with Pandas. To begin, install Pandas using this command: Copy pip install pandas Import pandas as pd In order to use the Pandas package in Python, you’ll need to type “import pandas as pd” at the top of your code. You’ll then be able to … Read more

Import numpy as np – Getting Started with NumPy

In this short guide, you’ll see how to get started with NumPy. First, install NumPy using the command: Copy pip install numpy Import numpy as np To use the NumpPy package, you’ll need to type “import numpy as np” at the top of your code. You’ll then be able to use NumPy functions and objects … Read more

Convert DataFrame to Matrix in R

To convert a DataFrame to a Matrix in R using the ‘as.matrix()‘ function: Copy mat <- as.matrix(df) For example: Copy # Create a DataFrame df <- data.frame( col_a = c(1, 2, 3), col_b = c(4, 5, 6), col_c = c(7, 8, 9) ) # Convert the DataFrame to a Matrix in R mat <- as.matrix(df) … Read more

Categories R

Convert Matrix to DataFrame in R

To convert a Matrix to a DataFrame in R using the as.data.frame() function: Copy df <- as.data.frame(my_matrix) Example 1: Convert Matrix to DataFrame Here we convert a matrix with 3 rows and 2 columns to a DataFrame (note that the class function was used to check the object type before and after the conversion): Copy … Read more

Categories R

How to Create a Matrix in R

The matrix() function can be used to create a matrix in R: (1) Create a numeric matrix with 3 rows and 2 columns – filled by columns (default): Copy my_matrix <- matrix(data = c(12, 15, 18, 23, 25, 27), nrow = 3, ncol = 2) print(my_matrix) The result: (2) Create a numeric matrix with 3 … Read more

Categories R

How to Check the Object Type in R

To check the object type in R using the class() function: (1) Numeric: Copy x <- 7 class(x) Output: (2) Character: Copy y <- “Hello world” class(y) Output: (3) Logical: Copy z <- TRUE class(z) Output: (4) Date: Copy d <- as.Date(“2024-02-16”) class(d) Output: (5) Integer: Copy i <- as.integer(7) class(i) Output: (6) Factor: Copy … Read more

Categories R

Remove Leading and Trailing Whitespace in R DataFrame

The trimws() function can be used to remove leading and trailing whitespace in R DataFrame: (1) To remove leading and trailing whitespace under a single column: Copy df$column_name <- trimws(df$column_name) (2) To remove leading and trailing whitespace across the entire DataFrame: Copy df[] <- lapply(df, trimws) Example 1: Remove Whitespace under a Single Column To … Read more

Categories R

View the Bottom Rows in R DataFrame using Tail

The tail() function can be used to view the bottom rows in R DataFrame: Copy tail(df, number of bottom rows needed) For example, to view the bottom 2 rows: Copy # Create a sample DataFrame df <- data.frame(product = c(“computer”, “monitor”, “keyboard”, “printer”, “tablet”), price = c(1200, 400, 100, 200, 300), brand = c(“A”, “B”, … Read more

Categories R

View the Top Rows in R DataFrame using Head

The head() function can be used to view the top rows in R DataFrame: Copy head(df, number of top rows needed) For example, to view the top 3 rows: Copy # Create a sample DataFrame df <- data.frame(product = c(“computer”, “monitor”, “keyboard”, “printer”, “tablet”), price = c(1200, 400, 100, 200, 300), brand = c(“A”, “B”, … Read more

Categories R

Filter Python List based on Conditions

Here are 5 ways to filter a Python list based on conditions: 1. Filter Numbers Greater Than a Threshold: Filter numbers greater than 20: Copy numbers = [7, 25, 9, 30, 15] # Filter numbers greater than twenty filtered_result = [n for n in numbers if n > 20] print(“Numbers greater than 20:”, filtered_result) The … Read more