我正在尝试创建一个pandas数据帧,它迭代地计算来自另一个数据帧的统计数据,它通过列(用正则表达式过滤)。如何创建结果数据帧?输入数据帧:
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我想要左杏仁核体积mm3~3。是值为-0.29729的行,列名为Cohen's d是每个Select_list的列:example, how dataframe should looks
发布于 2019-05-23 19:40:22
我在一个函数中写入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')发布于 2019-05-21 22:10:38
我仍然不能真正理解如何以及在哪里,但是您展示了在函数中的某个地方,您能够构建一个包含例如Left-Amygdala_Volume_mm3作为索引和-0.29729作为值的float64系列。我假设在同一时间,对于相同的索引值,有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 resulthttps://stackoverflow.com/questions/56235427
复制相似问题