python - 计算值并将值放入 M​​ultiIndex Pandas DataFrame 的二级列中

标签 python pandas dataframe data-manipulation multi-index

我有一个多索引 DataFrame,我想在其中放置一个名为 AB 的二级列。此二级列的值应等于每个样本的 AD[1]/DP,例如 sample 1 AB = 60/180

import pandas as pd
import numpy as np

genotype_data = [
                    ['0/1', '120,60', 180, 5, '0/1', '200,2', 202, 99],
                    ['0/1', '200,20', 60, 99, '0/1', '200,50', 250, 99],
                    ['0/1', '200,2', 202, 99, '0/1', '200,2', 202, 99] 
]


genotype_columns = [['Sample1', 'Sample2'], ['GT', 'AD', 'DP', 'GQ']]
cols = pd.MultiIndex.from_product(genotype_columns)
df = pd.DataFrame(data=genotype_data, columns=cols)

此代码生成以下输入文件/df:

   Sample1                        Sample2                       
GT      AD   DP  GQ      GT      AD   DP  GQ
0/1  120,60  180   5     0/1   200,2  202  99
0/1  200,20   60   3     0/1  200,50  250  99
0/1   200,2  202  99     0/1   200,2  202  99

期望的结果应该是:

      Sample1                        Sample2                       
GT      AD   DP  GQ    AB      GT      AD   DP  GQ    AB
0/1  120,60  180   5  0.33     0/1   200,2  202  99  0.01
0/1  200,20   60   3  0.33     0/1  200,50  250  99  0.20
0/1   200,2  202  99  0.01     0/1   200,2  202  99  0.01

我已经想出了一个解决方案,但它非常缓慢、低效并且依赖于循环。我需要一个更高效的解决方案,因为我将对非常大的文件执行此操作。

def calc_AB(df):

    sam = df.columns.levels[0][0]
    AD = df.xs('AD', level=1, axis=1).unstack().str.split(",", n=2)
    DP = df.xs('DP', level=1, axis=1).unstack()
    AB = round(pd.to_numeric(AD.str[1]) / pd.to_numeric(DP), 2)
    df[sam, 'AB'] = AB.tolist()

    return df 


dfs = [calc_AB(df[[sam]].astype(str)) for sam in df.columns.levels[0].tolist()]

pd.concat(dfs, axis=1) 

如有任何帮助,我们将不胜感激。

最佳答案

您需要重新组织索引以确保只有一个名为“AD”的列:

df.columns = df.columns.swaplevel(0,1)
stacked = df.stack()
#               AD   DP  GQ   GT    
#0 Sample1  120,60  180   5  0/1  
#  Sample2   200,2  202  99  0/1 
#1 Sample1  200,20   60  99  0/1 
#  Sample2  200,50  250  99  0/1 
#2 Sample1   200,2  202  99  0/1 
#  Sample2   200,2  202  99  0/1 

现在计算新列很简单:

stacked['AB'] = stacked['AD'].str.split(',').str[1].astype(int)/stacked['DP']

stacked
#               AD   DP  GQ   GT        AB
#0 Sample1  120,60  180   5  0/1  0.333333
#  Sample2   200,2  202  99  0/1  0.009901
#1 Sample1  200,20   60  99  0/1  0.333333
#  Sample2  200,50  250  99  0/1  0.200000
#2 Sample1   200,2  202  99  0/1  0.009901
#  Sample2   200,2  202  99  0/1  0.009901

如果需要,您可以将索引恢复到之前的状态。

关于python - 计算值并将值放入 M​​ultiIndex Pandas DataFrame 的二级列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43331221/

相关文章:

Python 正则表达式在论文中获取引用

Python:用urllib3超越SSL验证

python - 将 datetime64[ns] 转换为该周的给定日期的最有效方法?

python - 如何在 Pandas 中实现 rolling_pct_change(period)?

python - 在python中将打印语句与列表理解相结合

python - 根据pandas数据框的列值使用networkx创建图表

r - R中一列具有等差的子数据框

python - 获取值错误 : Shape of passed values is (14, 78692),索引暗示 (14, 78250)

python - 如何从数据帧中的列中减去标量值并仅选择满足某些条件的行

python - 用python求超越方程的根