1. Python副業(yè)200元-實現(xiàn)簡單的Excel統(tǒng)計函數(shù)

        共 5503字,需瀏覽 12分鐘

         ·

        2022-06-25 13:37

        剛剛吃完午飯打開python學(xué)習(xí)群看到螞蟻老師發(fā)布了一個新的兼職單子,正好這幾天剛看完excel辦公自動化的課程,趕緊接了單檢測一下自己學(xué)的怎么樣。

        需求分析

        根據(jù)原始數(shù)據(jù),計算出累計和、回撤、連續(xù)正確、連續(xù)錯誤、連續(xù)正確值與連續(xù)錯誤值6項數(shù)據(jù),其中原始數(shù)據(jù)大于等于0認(rèn)定為正確,原始數(shù)據(jù)小于0為錯誤。明白了要求,那我們就開始擼代碼吧~

        解決步驟

        import pandas as pd

        #創(chuàng)建一個計算數(shù)據(jù)的函數(shù)
        def calculate(df):
          pass

        #讀取原始數(shù)據(jù),將索引列去除
        df = pd.read_excel('需求0621.xlsx',index_col=0)
        #調(diào)用計算數(shù)據(jù)的函數(shù)
        calculate(df)

        先把整體思路寫好,再去想辦法計算每項數(shù)據(jù)

        #計算累計和
            lst1 = []
            sum = 0
            for i in range(df.shape[0]):
                if i == 0:
                    lst1.append(df['N'][i])
                    sum += df['N'][i]
                else:
                    sum += df['N'][i]
                    lst1.append(sum)
            df['累計和'] = lst1
        #計算回撤
            lst2 = []
            max = 0
            for i in range(df.shape[0]):
                if i == 0:
                    lst2.append(0)
                elif df['累計和'][i] > max:
                    max = df['累計和'][i]
                    lst2.append(0)
                elif df['累計和'][i] < max:
                    lst2.append(df['累計和'][i]-max)
                elif df['累計和'][i] == max:
                    lst2.append(0)
            df['回撤'] = lst2
        #計算連續(xù)正確的個數(shù)
            lst3 = []
            correct = 0
            for i in range(df.shape[0]):
                if df['N'][i] >= 0:
                    correct += 1
                    lst3.append(correct)
                else:
                    lst3.append(0)
                    correct = 0
            df['連續(xù)正確'] = lst3
        #計算連續(xù)錯誤的個數(shù)
            lst4 = []
            mistake = 0
            for i in range(df.shape[0]):
                if df['N'][i] < 0:
                    mistake += 1
                    lst4.append(mistake)
                else:
                    lst4.append(0)
                    mistake = 0
            df['連續(xù)錯誤'] = lst4
        #計算連續(xù)正確值
            lst5 = []
            for i in range(df.shape[0]):
                lst5.append('')
            right = 0
            for i in range(df.shape[0]):
                if df['連續(xù)正確'][i] != 0:
                    right += df['N'][i]
                elif df['連續(xù)正確'][i] == 0 and right != 0:
                    lst5[i-1] = right
                    right = 0
            df['連續(xù)正確值'] = lst5
        #計算連續(xù)錯誤值
            lst6 = []
            for i in range(df.shape[0]):
                lst6.append('')
            wrong = 0
            for i in range(df.shape[0]):
                if df['連續(xù)錯誤'][i] != 0:
                    wrong += df['N'][i]
                elif df['連續(xù)錯誤'][i] == 0 and wrong != 0:
                    lst6[i-1] = wrong
                    wrong = 0
            df['連續(xù)錯誤值'] = lst6

        最后將dataframe保存到excel

            df.to_excel('完成計算.xlsx')
            print('保持成功')

        最終結(jié)果

        技術(shù)總結(jié)

        雖然已經(jīng)完成了客戶的要求計算出了所有的數(shù)據(jù),但在寫代碼過程中計算的步驟都是基于python基礎(chǔ)語法實現(xiàn)的,對于pandas的使用還要繼續(xù)看看螞蟻老師的課程,只有通過大量的練習(xí)才能夠熟練的掌握

        最后感謝螞蟻老師的肯定,讓我在學(xué)習(xí)python的道路上又多了一份信心~

        今晚來螞蟻老師抖音直播間,Python帶副業(yè)全套餐有優(yōu)惠?。?!


        瀏覽 76
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
          
          

            1. 偷拍青娱乐 | 伊人东京热 | 青娱乐成人影院 | 四虎影视国产精品免费久久 | 五月婷婷丁香综合 |