按组,绘制最高分位数数据与最低分位数的数据,并捕获统计数据

斯科普

我希望通过“分析”对数据集进行分组,然后比较小细胞与大细胞的强度。我的问题是,在编写代码时,我仅了解如何对整个dataFrame的顶部和底部cellArea分位数进行分组,而不是针对每个单独的测定(“ wt”和“ cnt”)进行分组。

最后,我想比较每种测定类型的两组强度之间的平均值...

from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

df = DataFrame({'assay':['cnt']*10+['wt']*10,
                'image':['001']*10+['002']*5+['001']*5,
                'roi':['1']*5+['2']*5+['3']*5+['1']*5,
                'cellArea':[99,90,50,2,30,65,95,30,56,5,33,18,98,76,56,72,12,5,47,89],
                'intensity':[88,34,1,50,2,67,88,77,73,3,2,67,37,34,12,45,23,82,12,1]},
               columns=['assay','image','roi','cellArea','intensity'])

df.loc[(df['cellArea'] < df['cellArea'].quantile(.20)),'group'] = 'Small_CellArea'
df.loc[(df['cellArea'] > df['cellArea'].quantile(.80)),'group'] = 'Large_CellArea'
df = df.reset_index(drop=True)

sns.violinplot(data=df,y='intensity',x='assay',hue='group',capsize=1,ci=95,palette="Set3",inner='quartile',split=True, cut=0)
plt.ylim(-20,105)
plt.legend(loc='center', bbox_to_anchor=(0.5, 0.08), ncol=3, frameon=True, fancybox=True, shadow=True, fontsize=12)

在此处输入图片说明

在此处输入图片说明

智慧

按组计算低分位数和高分位数,然后将它们合并回到原始数据帧,然后可以从中计算group变量asSmalllarge

from pandas import pd
quantileLow = df.groupby('assay').cellArea.quantile(0.2).reset_index()
quantileHigh = df.groupby('assay').cellArea.quantile(0.8).reset_index()
df = pd.merge(df, pd.merge(quantileLow, quantileHigh, on = "assay"), on = "assay")

df.loc[df['cellArea'] < df.cellArea_x,'group'] = 'Small_CellArea'
df.loc[df['cellArea'] > df.cellArea_y,'group'] = 'Large_CellArea'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章