2017-02-18 90 views
1

如果這個問題太天真,請提前道歉。我是Python的新手。我試圖對我的數據框的兩列執行t檢驗。只有在同一數據幀中的另一列對列進行分組後,才能進行t檢驗。按小組分組給熊貓系列/數據框

我處理這樣的事情:

rand_array = np.random.randint(low=10, high=30, size=9) 
rand_array2 = np.random.randint(low=10, high=30, size=9) 
d = {'key1':[0,0,1,0,1,1,1,0,1], 'key2': rand_array, 'key3': rand_array2} 
df1 = pd.DataFrame(d) 
print df1 

我得到的輸出是:

 key1 key2 key3 
0  0 20 18 
1  0 22 16 
2  1 21 26 
3  0 21 13 
4  1 11 21 
5  1 23 10 
6  1 17 29 
7  0 13 25 
8  1 24 29 

然後,我按鍵1

g1 = df1.groupby('key1') 
print g1.groups 
>>> {0: Int64Index([0, 1, 3, 7], dtype='int64'), 1: Int64Index([2, 4, 5, 6, 8], dtype='int64')} 

我想執行一個基本上是0:Int64Index([0,1,3,7],dtype ='int64')與1:Int64Index([2,4,5,6,8],dtype ='int64')的t檢驗。

這可能嗎?

謝謝!

回答

2

Welch's T-Test

我會做這樣的:

def welch_ttest(x1, x2): 
    x_1 = x1.mean() 
    x_2 = x2.mean() 
    s1 = x1.std() 
    s2 = x2.std() 
    n1 = len(x1) 
    n2 = len(x2) 
    return ((x_1 - x_2)/(np.sqrt(s1 ** 2/n1 + s2 ** 2/n2))) 

def grouped_welch_ttest(df): 
    return welch_ttest(df.key2, df.key3) 

df1.groupby('key1').apply(grouped_welch_ttest) 

key1 
0 -1.471497 
1 1.487045 
dtype: float64 
+1

這在['scipy.stats.ttest_ind'(https://docs.scipy.org/doc還實施/scipy-0.14.0/reference/generated/scipy.stats.ttest_ind.html)和'equal_var = False'。 – unutbu

+0

謝謝你們倆。它現在工作:) – tester777