简体   繁体   中英

Only obtain p-value from t-test error when selecting subset

I found this thread Ignore python multiple return value but I still don't understand how it would be possible to only obtain p-value from a t-test. I'm trying to express just the p-values in a vector and I'm really struggling. I tried the fun()[1]etc method but it returns an empty index.

My code looks like this:

# Statistical analysis: unpaired t-test
import scipy
from scipy import stats
# Sorting for dosage
drug_1_1 = table.iloc[:,1][(table['Dose drug 1'] == 1)]
drug_2_1 = table.iloc[:,3][(table['Dose drug 2'] == 1)]
drug_1_2 = table.iloc[:,1][(table['Dose drug 1'] == 2)]
drug_2_2 = table.iloc[:,3][(table['Dose drug 2'] == 2)]
drug_1_3 = table.iloc[:,1][(table['Dose drug 1'] == 3)]
drug_2_3 = table.iloc[:,3][(table['Dose drug 2'] == 3)]
drug_1_4 = table.iloc[:,1][(table['Dose drug 1'] == 4)]
drug_2_4 = table.iloc[:,3][(table['Dose drug 2'] == 4)]
drug_1_5 = table.iloc[:,1][(table['Dose drug 1'] == 5)]
drug_2_5 = table.iloc[:,3][(table['Dose drug 2'] == 5)]
drug_1_6 = table.iloc[:,1][(table['Dose drug 1'] == 6)]
drug_2_6 = table.iloc[:,3][(table['Dose drug 2'] == 6)]
drug_1_7 = table.iloc[:,1][(table['Dose drug 1'] == 7)]
drug_2_7 = table.iloc[:,3][(table['Dose drug 2'] == 7)]
drug_1_8 = table.iloc[:,1][(table['Dose drug 1'] == 8)]
drug_2_8 = table.iloc[:,3][(table['Dose drug 2'] == 8)]

# Expessing p-values in vector
P_values = pd.DataFrame()
P_values['1'] = stats.ttest_ind(drug_1_1,drug_2_1)[1]
P_values['2'] = stats.ttest_ind(drug_1_2,drug_2_2)[1]
P_values['3'] = stats.ttest_ind(drug_1_3,drug_2_3)[1]
P_values['4'] = stats.ttest_ind(drug_1_4,drug_2_4)[1]
P_values['5'] = stats.ttest_ind(drug_1_5,drug_2_5)[1]
P_values['6'] = stats.ttest_ind(drug_1_6,drug_2_6)[1]
P_values['7'] = stats.ttest_ind(drug_1_7,drug_2_7)[1]
P_values['8'] = stats.ttest_ind(drug_1_8,drug_2_8)[1]
P_values.index.names = ['Dose']
print(P_values)

Which returns:

Empty DataFrame
Columns: [1, 2, 3, 4, 5, 6, 7, 8]
Index: []

Which stragenly seems to work if it's not played in the first line, but it returns the same value for both 0 and 1 in all other lines like so: https://imgur.com/a/9H7YXL7

Am I writing something wrong?

So, based on what you've written so far, it looks like you're doing a lot of (possibly) unecessary hard-coding of values and storing of variables. If you need to re-use all of your drug_1_1 etc, then leave the upper portion of the code as is. If you don't need to use those later, then you should be able to loop over the range of values and do all of your lookups, ttesting, and pval storing at once as below: (Note, I couldn't test this, as I have no idea what your data looks like)

p_vals=[]
for i in range(1,9):
    p_vals.append(stats.ttest_ind(table.iloc[:,1][(table['Dose drug 1'] == i)],
                                  table.iloc[:,3][(table['Dose drug 2'] == i)])[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM