[英]How to iteratively fill pandas Dataframe with columns
我正在嘗試創建一個pandas數據框,並從另一個數據框中迭代統計statisitcs,它通過列(使用正則表達式過濾)。 我如何創建結果數據框? 輸入數據框:
In [4]: control.head()
Out[4]:
Patient Gender Age Left-Lateral-Ventricle_NVoxels Left-Inf-Lat-
Vent_NVoxels ... supramarginal_CurvInd_lh
0 P008 M 30 9414
311 ... 7.5
1 P013 F 35 7668
85 ... 10.4
2 P018 F 27 7350
202 ... 8.0
3 P033 F 55 7548
372 ... 9.2
4 P036 F 31 8598
48 ... 8.0
[5 rows x 930 columns]
我寫了一個代碼來統計統計信息,但堅持創建結果熊貓數據框
def select_volumes(group_c,group_k):
Select_list = ["Amygdala", "Hippocampus", "Lateral-Ventricle",
"Pallidum", "Putamen", "Thalamus"]
Side = ["Left", "Right"]
for s in Side:
for struct in Select_list:
volumes_c = group_c.filter(regex="^(?=.*"+s+")(?=.*"+struct+")
(?=.*Volume)")
volumes_k = group_k.filter(regex="^(?=.*"+s+")(?=.*"+struct+")
(?=.*Volume)")
k = cohens_d(volumes_c, volumes_k)
meand = volumes_c.mean()
result_df = pd.Dataframe(
{
"Cohen's norm": some result
"Mean Value": meand
}
)
return k
函數select_volumes給我結果:
Left-Amygdala_Volume_mm3 -0.29729
dtype: float64
Left-Hippocampus_Volume_mm3 0.33139
dtype: float64
Left-Lateral-Ventricle_Volume_mm3 -0.111853
dtype: float64
Left-Pallidum_Volume_mm3 0.28857
dtype: float64
Left-Putamen_Volume_mm3 0.696645
dtype: float64
Left-Thalamus-Proper_Volume_mm3 0.772492
dtype: float64
Right-Amygdala_Volume_mm3 -0.358333
dtype: float64
Right-Hippocampus_Volume_mm3 0.275668
dtype: float64
Right-Lateral-Ventricle_Volume_mm3 -0.092283
dtype: float64
Right-Pallidum_Volume_mm3 0.279258
dtype: float64
Right-Putamen_Volume_mm3 0.484879
dtype: float64
Right-Thalamus-Proper_Volume_mm3 0.809775
dtype: float64
我希望Left-Amygdala_Volume_mm3 ...是值為-0.29729且行名為Cohen's d的行作為每個Select_list的列: 例如,數據幀的外觀
我仍然無法真正理解操作的方式和位置,但是您表明該函數中的某個位置能夠構建一個float64系列,其中包含例如Left-Amygdala_Volume_mm3
作為索引,而-0.29729
作為值。 而且我假設您同時具有相同索引值的meand
值。
更確切地說,我將假設:
k = pd.Series([-0.29729], dtype=np.float64,index=['Left-Amygdala_Volume_mm3'])
因為它打印為:
print(k)
Left-Amygdala_Volume_mm3 -0.29729
dtype: float64
同時,我認為它的meand
也是相似的系列。 因此,我們將其訪問值為meand.iloc[0]
(假設值為9174.1)。
您應該將它們結合起來以構建一行的內容:
row = k.reset_index().iloc[0].tolist() + [meand.iloc[0]]
在示例中,我們具有以下row
: ['Left-Amygdala_Volume_mm3', -0.29729, 9174.1]
因此,您現在需要構建該行的大型列表:
def select_volumes(group_c,group_k):
Select_list = ["Amygdala", "Hippocampus", "Lateral-Ventricle",
"Pallidum", "Putamen", "Thalamus"]
Side = ["Left", "Right"]
data = []
for s in Side:
for struct in Select_list:
volumes_c = group_c.filter(regex="^(?=.*"+s+")(?=.*"+struct+")
(?=.*Volume)")
volumes_k = group_k.filter(regex="^(?=.*"+s+")(?=.*"+struct+")
(?=.*Volume)")
k = cohens_d(volumes_c, volumes_k)
meand = volumes_c.mean()
# build a row of result df
data.append(k.reset_index().iloc[0].tolist() + [meand.iloc[0]])
# after the loop combine the rows into a dataframe and return it:
result = pd.DataFrame(data, columns=['index', "Cohen's d", 'Mean']).set_index('index')
return result
我在函數內寫入pd.Dataframe:
k = cohens_d(volumes_c, volumes_k)
meand = volumes_c.mean()
volumes_df.append([cohen.index[0],cohen.values[0], meand)
return volumes_df
並從函數中調用pd.Dataframe與:
finaldf=pd.DataFrame(select_volumes(control,patolog))
finaldf.columns=['Structure','Cohensd','Meand')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.