gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:17 28 4
gpt4 key购买 nike

我希望按“测定”对数据集进行分组,然后比较小细胞与大细胞的强度。我遇到的问题是,在编写代码时,我只了解如何对整个数据帧的顶部和底部 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)

enter image description here

enter image description here

最佳答案

按组计算低分位数和高分位数,然后将它们合并回原始数据帧,然后您可以将 group 变量计算为 Small:

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'

关于python - 按组绘制最高分位数数据与最低分位数数据,并捕获统计数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38043724/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com