Python 在迭代过程中求和拆分的数据帧列

Python 在迭代过程中求和拆分的数据帧列,python,pandas,iteration,Python,Pandas,Iteration,我有一个数据帧“fpd”,它在列['View']中使用 bookuniques = fpd['View'].unique() fpdict = {elem: pd.DataFrame for elem in bookuniques} for key in fpdict.keys(): fpdict[key] = fpd[:][fpd['View'] == key] 数据帧看起来像: Product PG Location Row Group Ph DD

我有一个数据帧“fpd”,它在列
['View']
中使用

bookuniques = fpd['View'].unique()

fpdict = {elem: pd.DataFrame for elem in bookuniques}

for key in fpdict.keys():
    fpdict[key] = fpd[:][fpd['View'] == key]
数据帧看起来像:

    Product PG Location Row Group   Ph DD                   Pd TC   Variance    
    C4      CL          01.1 OI     OpeningInventory        200     200     
            PU          01.1 OI     OpeningInventory        400     400
            MR          01.1 OI     OpeningInventory        600     600 
            NP          01.1 OI     OpeningInventory        200     200
            PR          01.1 OI     OpeningInventory        400     400 
            PS          01.1 OI     OpeningInventory        600     600 
            PW          01.1 OI     OpeningInventory        200     200 
我试图分别为这些数据帧中的每一个追加一个求和行。我已尝试使用将流程包含到excel的输出中

with pd.ExcelWriter('check2.xlsx') as writer:
    for key in fpdict.keys():
        fpdict[key].drop(['View'], axis = 1) 
        fpdict[key].append(fpdict[key].sum(numeric_only = True), ignore_index=True)
        temp = fpdict[key]
        temp.to_excel(writer, sheet_name = key)
不幸的是,这样做会删除索引列
[['Product']、['PG']、['Location']]

我希望输出是

        Product PG Location Row Group   Ph DD                   Pd TC   Variance    
        C4      CL          01.1 OI     OpeningInventory        200     200     
                PU          01.1 OI     OpeningInventory        400     400
                MR          01.1 OI     OpeningInventory        600     600 
                NP          01.1 OI     OpeningInventory        200     200
                PR          01.1 OI     OpeningInventory        400     400 
                PS          01.1 OI     OpeningInventory        600     600 
                PW          01.1 OI     OpeningInventory        200     200
                TOTAL                                           2600    2600    

以下是我必须做的假设,因为这在问题中没有明确说明:

  • dataframe在Product、PG和Location列上有一个多索引
  • 新行将PG=Total和所有其他非数字字段设置为空字符串
  • fpdict[key]将删除
    视图
您必须将代码更改为:

with pd.ExcelWriter('check2.xlsx') as writer:
    for key in fpdict.keys():
        temp = fpdict[key].drop(['View'], axis = 1).reset_index()
        temp.append(fpdict[key].sum(numeric_only = True), ignore_index=True) # add sum row
        temp.iloc[-1] = temp.iloc[-1].fillna(' ')      # replace NaNs with ''
        temp.iloc[-1, 1] = 'TOTAL'
        fpdict[key] = temp.set_index(['Product', 'PG', 'Location'])
        temp.to_excel(writer, sheet_name = key)

您是否尝试过
groupby
fpd.groupby('View').sum()
?@SergeBallesta我还没有-我需要删除我的分层索引才能使用它吗?@SergeBallesta update-尝试了它,但没有任何结果。如果没有,我们将无法提供帮助…@SergeBallesta我已经复制了输出并记录了所需的输出。我会尝试一下-至于这个问题,我很想知道我怎样才能做得更好。我不确定如何突出显示单个单词,以直观地将它们标识为索引-我尝试了格式化,但它变得很混乱。是否有用于格式化数据帧的资源?我将确保下次为其他字段指定所需的输出-我认为问题很清楚,但我理解为什么会让人困惑。我还意识到我漏掉了视图列-抱歉。我唯一需要做的编辑是在temp.append行前面加上temp=。谢谢你的帮助!此外,我必须将temp.to_excel改为fpdict[key]改为excel,否则输出不会保留索引格式