<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【Python】用 Python 幫財(cái)務(wù)小妹生成 Excel 報(bào)表,小妹直說(shuō)一輩子。。。

          共 2683字,需瀏覽 6分鐘

           ·

          2021-12-09 15:37

          財(cái)務(wù)小妹

          蘿卜哥,求助!

          人有三急,回見(jiàn)~

          蘿卜


          財(cái)務(wù)小妹

          別啊,不能去!

          這,到底啥事啊

          蘿卜

          財(cái)務(wù)小妹

          有一個(gè)超級(jí)繁瑣的事情,不過(guò)感覺(jué)你和Python能幫我

          那你學(xué)Python吧,畢竟我也不能幫你一輩子

          蘿卜

          財(cái)務(wù)小妹

          不一定哦,先幫我把這次搞定,也許能一輩子呢

          當(dāng)真?我來(lái)看看

          蘿卜

          財(cái)務(wù)小妹的需求


          有一個(gè)Excel數(shù)據(jù),需要根據(jù)一定規(guī)則重新組合數(shù)據(jù),并把不同的數(shù)據(jù)發(fā)給不同的人


          原始數(shù)據(jù):



          目的數(shù)據(jù):



          每一個(gè)tab頁(yè)的數(shù)據(jù),直接發(fā)給對(duì)應(yīng)的人即可!


          對(duì)于上面的數(shù)據(jù)格式轉(zhuǎn)化,我們可以很自然的想到使用透視表,pivot table


          然后為了達(dá)到更好的效果,我們還使用了 Pandas 當(dāng)中的?DataFrame.xs?函數(shù)


          先來(lái)進(jìn)行透視表轉(zhuǎn)換

          df = pd.read_excel("sales-funnel.xlsx")table = pd.pivot_table(df,index=["Manager","Rep","Product"],               values=["Price","Quantity"],               aggfunc=[np.sum,np.mean],fill_value=0)?table


          接下來(lái)應(yīng)用 xs

          table.xs('Debra Henley', level=0)


          可以看到很輕松的就拿到了?Debra Henley 對(duì)應(yīng)的數(shù)據(jù)


          我們還可以繼續(xù)向下鉆取數(shù)據(jù)

          table.xs(('Debra Henley','Craig Booker'), level=0)


          接下來(lái)我們還需要用到?get_level_values?函數(shù)

          table.index.get_level_values(0)table.index.get_level_values(1)table.index.get_level_values(0).unique()

          Output:

          Index(['Debra Henley', 'Debra Henley', 'Debra Henley', 'Debra Henley',       'Debra Henley', 'Debra Henley', 'Debra Henley', 'Fred Anderson',       'Fred Anderson', 'Fred Anderson', 'Fred Anderson', 'Fred Anderson',       'Fred Anderson'],??????dtype='object',?name='Manager')
          Index(['Craig Booker', 'Craig Booker', 'Craig Booker', 'Daniel Hilton',       'Daniel Hilton', 'John Smith', 'John Smith', 'Cedric Moss',       'Cedric Moss', 'Cedric Moss', 'Wendy Yule', 'Wendy Yule', 'Wendy Yule'],      dtype='object', name='Rep')
          Index(['Debra Henley', 'Fred Anderson'], dtype='object', name='Manager')


          下面我們就可以寫(xiě)一個(gè)循環(huán),依次獲取到 manager 所需要的數(shù)據(jù)

          for manager in table.index.get_level_values(0).unique():    print(table.xs(manager, level=0))


          最后把數(shù)據(jù)保存到新的 Excel 當(dāng)中

          writer = pd.ExcelWriter('output.xlsx')
          for manager in table.index.get_level_values(0).unique(): temp_df = table.xs(manager, level=0) temp_df.to_excel(writer,manager)
          writer.save()


          由于上述操作都是在 Jupyter?Notebook 當(dāng)中進(jìn)行的,下面我們把相關(guān)代碼封裝下,通過(guò)命令行來(lái)執(zhí)行

          if __name__ == "__main__":    parser = argparse.ArgumentParser(description='Script to generate sales report')    parser.add_argument('infile', type=argparse.FileType('r'),                        help="report source file in Excel")    parser.add_argument('outfile', type=argparse.FileType('w'),                        help="output file in Excel")    args = parser.parse_args()    # We need to pass the full file name instead of the file object    sales_report = create_pivot(args.infile.name)    save_report(sales_report, args.outfile.name)


          至此,上述需求全部完成!


          財(cái)務(wù)小妹

          哇,好棒哦~

          嘿嘿,一般一般

          蘿卜


          財(cái)務(wù)小妹

          嗯嗯,那我先下班嘍

          好呀,話說(shuō)咱們什么時(shí)候說(shuō)說(shuō)一輩子的事情

          蘿卜


          財(cái)務(wù)小妹

          沒(méi)問(wèn)題啊,咱們要做一輩子的同事

          。。。

          蘿卜


          好了,今天就到這里了,為了幫忙撫平蘿卜哥受傷的小心靈,點(diǎn)個(gè)再走吧


          往期精彩回顧




          站qq群955171419,加入微信群請(qǐng)掃碼:
          瀏覽 49
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  99爱在线播放 | 亚洲国产成人无码a在线播放 | 中国农村乱婬片AA片 | 国产精品九九九九九九九九 | 人人摸人人看人人草 |