日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

【Python】用 Python 帮财务小妹生成 Excel 报表,小妹直说一辈子。。。

發(fā)布時(shí)間:2025/3/12 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python】用 Python 帮财务小妹生成 Excel 报表,小妹直说一辈子。。。 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

財(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')

下面我們就可以寫一個(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 objectsales_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è)再走吧

往期精彩回顧適合初學(xué)者入門人工智能的路線及資料下載機(jī)器學(xué)習(xí)及深度學(xué)習(xí)筆記等資料打印機(jī)器學(xué)習(xí)在線手冊(cè)深度學(xué)習(xí)筆記專輯《統(tǒng)計(jì)學(xué)習(xí)方法》的代碼復(fù)現(xiàn)專輯 AI基礎(chǔ)下載黃海廣老師《機(jī)器學(xué)習(xí)課程》視頻課黃海廣老師《機(jī)器學(xué)習(xí)課程》711頁(yè)完整版課件

本站qq群955171419,加入微信群請(qǐng)掃碼:

總結(jié)

以上是生活随笔為你收集整理的【Python】用 Python 帮财务小妹生成 Excel 报表,小妹直说一辈子。。。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。