I want to calculate the scipy.stats.ttest_ind() for numeric columns in a pandas DataFrame with the binary target variable.
import pandas as pd
from scipy import stats
def calculate_tStatistic(df, target, numeric_cols):
"""
Calculate the t-test on TWO RELATED samples of scores, a and b.
This is a two-sided test for the null hypothesis that 2 related or
repeated samples have identical average (expected) values.
Params
=====================================
df : dataframe
target : target column name(str)
numeric_cols : list of numeric columns
Return
=======================================
results_df : A dataframe with features and t-statistic p-val
"""
# create an empty dictionary
t_test_results = {}
# loop over column_list and execute code
for column in numeric_cols:
group1 = df.where(df[target] == 0).dropna()[column]
group2 = df.where(df[target] == 1).dropna()[column]
# add the output to the dictionary
t_test_results[column] = stats.ttest_ind(group1,group2)
results_df = pd.DataFrame.from_dict(t_test_results,orient='Index')
results_df.columns = ['t-statistic','t_stat_pval']
results_df.reset_index(inplace=True)
results_df.rename(columns = {"index":"x"}, inplace=True)
return results_df
Sample
import seaborn as sns
titanic = sns.load_dataset("titanic")
calculate_tStatistic(titanic, "survived", ["age", "fare"])
Results
x t-statistic t_stat_pval
0 age 3.479558 0.000630
1 fare -1.767759 0.078795
Can someone confirm that I'm actually doing it the right way?
Is there a better / more elegant / more accurate way to do this??