我有一个pandas groupby对象,它返回每种基因类型的计数,大致如下所示(为清晰起见,手动设置了列标题):
counts = df.groupby(["ID", "Gene"]).size()
counts
ID Gene Count
1_1_1 SMARCB1 1
smad 12
1_1_10 SMARCB1 2
smad 17
1_1_100 SMARCB1 3
我需要获取zscore组内的内容,然后返回具有最高zscore的基因。
我尝试了以下操作,但似乎正在整个数据集中计算zscores,但未返回正确的zscore:
zscore = lambda x: (x - x.mean()) / x.std()
counts = df.groupby(["ID", "Match"]).size().pipe(zscore)
我已经尝试过转换并获得相同的结果。
我试过了:
counts = match_df.groupby(["ID", "Match"]).size().apply(zscore)
这给了我以下错误:
'int' object has no attribute 'mean'
无论我尝试什么,它都不会给出正确的输出。前两行的zscores应该为[-1,1],在这种情况下,我将返回1_1_1 SMARCB1的行。等等,谢谢!
更新资料
感谢@ZaxR的帮助并切换到numpy均值和标准差,如下所示,我能够解决此问题。此解决方案还提供每个基因的原始计数和zscores的摘要数据框:
# group by id and gene match and sum hits to each molecule
counts = df.groupby(["ID", "Match"]).size()
# calculate zscore by feature for molecule counts
# features that only align to one molecule are given a score of 1
zscore = lambda x: (x - np.mean(x)) / np.std(x)
zscores = counts.groupby('ID').apply(zscore).fillna('1').to_frame('Zscore')
# group results back together with counts and output to
# merge with positions and save to file
zscore_df = zscores.reset_index()
zscore_df.columns = ["ID", "Match", "Zscore"]
count_df = counts.reset_index()
count_df.columns = ["ID", "Match", "Counts"]
zscore_df["Counts"] = count_df["Counts"]
# select gene with best zscore meeting threshold
max_df = zscore_df[zscore_df.groupby('ID')['Zscore'].transform(max) \
== zscore_df['Zscore']]
最佳答案
df.groupby(["ID", "Gene"]).size().transform(zscore)
不起作用的原因是因为最后一组是只有一个项目的系列,因此当您尝试将lambda函数zscore应用于单个[integer]时,会出现'int' object has no attribute 'mean'
错误。请注意,x.mean()的行为不同于熊猫的“ mean”。
更新资料
我认为应该这样做:
# Setup code
df = pd.DataFrame({"ID": ["1_1_1", "1_1_1", "1_1_10", "1_1_10", "1_1_100"],
"Gene": ["SMARCB1", "smad", "SMARCB1", "smad", "SMARCB1"],
"Count": [1, 12, 2, 17, 3]})
df = df.set_index(['ID', 'Gene'])
# Add standard deviation for every row
# Note: .transform(zscore) would also work
df['std_dev'] = df.groupby('ID')['Count'].apply(zscore)
# Find the max standard deviation for each group and
# use that as a mask for the original df
df[df.groupby('ID')['std_dev'].transform(max) == df['std_dev']]
Out:
Count std_dev
ID Gene
1_1_1 smad 12 0.707107
1_1_10 smad 17 0.707107