How to Set Multicolumn Index in Pandas

In pandas DataFrame, you can set multiple columns as an index using the set_index() function. The basic synatx for set_index() function is: df = df.set_index(['col1', 'col2']) The following examples demonstrate how to use the set_index() function to create an index from multiple columns in pandas DataFrame. Create a sample pandas DataFrame, # import package import pandas as pd df = pd.DataFrame({'col1': [1, 2, 3, 4, 6], 'col2': ['A', 'B', 'A', 'C', 'D'], 'col3': [4, 5, 6, 10, 12]}) # view DataFrame df col1 col2 col3 0 1 A 4 1 2 B 5 2 3 A 6 3 4 C 10 4 6 D 12 By default, the pandas DataFrame automatically assigns the default integer index starting from 0 and incrementing by 1 for each row.

Two-sample Kolmogorov-Smirnov (KS) Test in Python

In Python, the ks_2samp() function (from scipy) can be used for performing the two-sample Kolmogorov-Smirnov (KS) test. The Kolmogorov-Smirnov (KS) test is a nonparametric test that assesses if two samples come from the same distribution. The basic syntax for ks_2samp() function is: from scipy import stats stats.ks_2samp(sample1, sample2) The following examples demonstrate how to perform a Kolmogorov-Smirnov test in python. Create dataset Create datasets with normal distributions for two samples ,

Two-sample t-test with Unequal Sample Sizes in Python

The two-sample t-test with unequal sample sizes can be performed using the ttest_ind() function from the Scipy package in Python. In case of unequal sample sizes, you should check the assumption of the equality of variances (homoscedasticity). If the variances are not equal you should perform the Welch’s t-test (which does not assume equal variances between the two samples). You should use Student’s two-sample t-test with unequal sample sizes when variances are equal

Two-sample t-test with Unequal Sample Sizes in R

The two-sample t-test with unequal sample sizes can be performed using the built-in t.test() function from in R. In case of unequal sample sizes, you should check the assumption of the equality of variances (homoscedasticity). If the variances are not equal you should perform the Welch’s t-test (which does not assume equal variances between the two samples). You should use Student’s two-sample t-test with unequal sample sizes when variances are equal

Find and Replace Values in List in Python

You can find and replace string values in a list using a list comprehension or map() function in Python. Method 1: list comprehension new_list = [s.replace('old_string', 'new_string') for s in input_list] Method 2: map() function new_list = map(lambda s: str.replace(s, 'old_string', 'new_string'), input_list) The following examples demonstrate how to use list comprehension and map() functions to replace string values in a list in Python. Example 1: find and replace string using list comprehension Create a sample list,

How to Extract String Between Two Characters or Strings in Python

You can extract a string between two characters or strings in Python using various functions such as search() (from re package) and split() functions. Method 1: search() function # import package import re m = re.search('string1(.*)string2', input_string) ext_string = m.group(1) Method 2: split() function input_string.split('string1')[1].split('string2')[0] The following examples demonstrate how to use search() and split() functions to extract a string between two characters or strings in Python. Example 1: Extract string between two characters using search() Create a simple string,