I'm currently working with a dataframe in the form:
import pandas as pd
import numpy as np
df = pd.DataFrame([['A', 12.1, 11.4, 15.1, 9.9], ['B', 8.3, 10.3, 16.6, 7.8], ['B', 7.8, 11.1, 16.3, 8.4],
['B', 8.6, 10.9, 16.4, 8.1], ['A', 12.25, 11.6, 16.25, 8.9], ['B', 8.13, 11.6, 16.7, 7.4]
], columns = ['Symbol', 'C1','C2', 'C3', 'C4'])
And a list of lists that includes the comparisons across columns I'd like to make:
lst = [['C1','C2'], ['C1','C3'], ['C3','C4']]
I'm trying to calculate the difference of the means (repeated symbols) for each comparison as well as do a ttest_ind then return a new dataframe with the results that would look like:
df2 = pd.DataFrame([['A', 0.675, 'pval here', -3.5, 'pval here',6.275,'pval here'],
['B', -2.7675, 'pval here', -8.2925, 'pval here', 8.575 , 'pval here']],
columns = ['Symbol', 'C1-C2','C1-C2 pval', 'C1-C3', 'C1-C3 pval', 'C3-C4','C3-C4 pval'])
Finding the difference between the means is somewhat straightforward using groupby to get the means then loop over the pairs of the list as:
df = df.groupby('Symbol').agg(np.mean)
for pair in lst:
df[pair[0]+'-'+pair[1]] = df[pair[0]] - df[pair[1]]
But I've been stuck in applying ttest_ind and then returning the p-vaule into another column.
Any assistance is greatly appreciated.