Examples of random.seed( ) in Python

random.seed() function can help save the state of random functions. Thus, by using seed(), these random functions can generate the same numbers on multiple code executions. Example 1 Example 1 shows how to use random.seed() and how it impacts the generated numbers. Note that, random.random() generates a floating point number in the range 0.0 <= X < 1.0. The following … Read more

When to Use ddof=1 in np.std()

The following is the rule of using ddof in np.std() in Numpy. Rule 1: If you are calculating standard deviation for a sample, set ddof = 1 in np.std(). np.std(sample_name, ddof=1) Rule 2: If you are calculating standard deviation for a population, set ddof = 0 in np.std(). np.std(population_name, ddof=0) Example of ddof = 1 … Read more

Generate Random Numbers in Python

This tutorial shows how you can use Numpy to generate random numbers in Python. The following is the basic syntax summarizing 3 functions. 1. Integers: np.random.randint() 2. Normal distribution: np.random.randn() 3. Uniform distribution: np.random.rand() Example 1: Integer np.random.randint(low, high=None, size=None, dtype=int) np.random.randint() will return integer numbers. Given that there are quite a few parameters in randint(), it is … Read more

How to Round Numbers in Pandas

You can use round() and apply() to round up and down numbers in Pandas. Round to specific decimal places: df.round(decimals = number of specific decimal places) Round up numbers: df[‘DataFrame column’].apply(np.ceil) Method 3: Round down values: df.apply(np.floor) Data being used The following is a column of numbers that we are going to use in this … Read more

How to Create an Empty Pandas Dataframe

You can use DataFrame() to create an empty Pandas dataframe. The following is the basic syntax as well as two examples. import pandas as pd df = pd.DataFrame() Example 1 The following creates an empty dataframe in Pandas and prints it out. The following is the output. As we can see, both columns and indexes … Read more

How to Replace NaN with Zero in Pandas

You can replace NaN with zero using either fillna(0) in Pandas or replace(np.nan,0) in Numpy. Single Column: Method 1: df[‘Column_name’].fillna(0) Method 2: df[‘Column_name’].replace(np.nan,0) Whole dataframe: Method 1: df.fillna(0) Method 2: df.replace(np.nan,0) Example 1: single column The following Python code first creates a dataframe with NaN in both columns and then replaces NaN in the first … Read more

Read CSV without the first column in Python

This tutorial includes two methods to read CSV without the first column in Python. Method 1: pd.read_csv(“CSV_file_name”,index_col=0) Method 2: df=pd.read_csv(“CSV_file_name”) del df[df.columns[0]] Example for Method 1 The following is an example showing without and with index_col=0 in read.csv(). The following is the output, which includes both versions of dataframe. In the removed dataframe, we remove … Read more

Difference between Population Variance and Sample Variance

The difference between population variance and sample variance is on the denominator of the formula. In particular, the denominator for population variance is N, whereas sample variance is n-1. The following uses formulas and examples to explain the difference between them. Data Example The following are 5 numbers that we are going to calculate variance. … Read more

Print Current Working Directory in Jupyter Notebook

This tutorial shows how you can print out the current working directory in Jupyter Notebook. In particular, you can use os.getcwd() to do it and this tutorial provides the complete Python code. In particular, the following is the Python code. The OS module provides functions for interacting with the operating system in Python and thus … Read more

Convert CSV to Excel in Python

This tutorial shows how to convert CSV file to Excel in Python with examples and detailed steps. The following shows the specific steps. Steps of Convert CSV to Excel in Python Step 1 Install Pandas Pandas is a commonly used Python package and we can use it to convert CSV to Excel files. If your … Read more