我有一個帶有多索引的Pandas數據框。等級0是'應變',等級1是'JGI庫'。每個「Strain」都有幾個與之關聯的「JGI庫」列。我想使用lambda函數來應用t檢驗來比較兩個不同的菌株。爲了排除故障,我使用.iloc [0]命令佔用了一行數據幀。在Pandas lambda函數中訪問組
row = pvalDf.iloc[0]
parent = 'LL1004'
child = 'LL345'
ttest_ind(row.groupby(level='Strain').get_group(parent), row.groupby(level='Strain').get_group(child))[1]
這個按預期工作。現在我試着將它應用到我的整個數據幀
parent = 'LL1004'
child = 'LL345'
pvalDf = countsDf4.apply(lambda row: ttest_ind(row.groupby(level='Strain').get_group(parent), row.groupby(level='Strain').get_group(child))[1])
現在,我得到一個錯誤信息說,「ValueError異常:(‘級別名稱應變是不是索引的名字’,「發生在指數(LL1004,BCHAC )')「
'LL1004'是'Strain',但是Pandas似乎並沒有意識到這一點。它看起來也許multiindex沒有正確傳遞給lambda函數?是否有更好的方法來解決lambda函數比使用.iloc [0]?
我把我Jupyter筆記本的副本和一個excel與countsDf4數據幀文件在Github上https://github.com/danolson1/pandas_ttest
感謝, 丹
'axis = 1'解決了這個問題。並感謝其他評論。 –