Python練手,造樣本

來源:互聯網
上載者:User

標籤:pytho   最大的   imp   [1]   frame   pandas   panda   continue   編號   

import pandas as pddef getEmpDataFrame(num):        '''建立一份可複用的資料,有一定的隨機性和真實性'''        #員工編號    emp = [''] * num    lenNum = len(str(num))    for i in range(num):        emp[i] = str(i+1).zfill(lenNum)            #性別:男多女少    sex = [1] * int(round(num*0.7)) + [0] * (num - int(round(num*0.7)))        #年齡:年齡是平均分布的,相對來說男同事比女同事要大些    age = [0] * num    for i in range(num):        if sex[i] == 1 :            age[i] = 28 + i % 33        if sex[i] == 0 :            age[i] = 22 + i % 33                #職級:越進階越罕有  與其他因素無關    lvl = [0] * num    for i in range(num):        if lvl[i] ==0 and (i+1) % 33 == 0 :            lvl[i] = 5             continue        if lvl[i] ==0 and (i+1) % 23 == 0 :            lvl[i] = 4            continue        if lvl[i] ==0 and (i+1) % 13 == 0 :            lvl[i] = 3            continue        if lvl[i] ==0 and (i+1) % 3 == 0 :            lvl[i] = 2            continue        lvl[i] = 1            #入職年長:跟職級和年齡有關  #通常4年升1級  #年齡-最低年齡=可能最大的入職年長    yrs = [0] * num    for i in range(num):                if sex[i] == 1 :            if lvl[i] * 4 >= age[i] - 28 :                yrs[i] = age[i] - 28             else:                yrs[i] = lvl[i] * 4          if sex[i] == 0 :            if lvl[i] * 4 >= age[i] - 22 :                yrs[i] = age[i] - 22             else:                yrs[i] = lvl[i] * 4         #學曆:年齡小的平均學曆相對高些,職級高的學曆相對高些    edu = [0] * num    for i in range(num):        if lvl[i] == 5 or lvl[i] == 4 :            if age[i] < 40 :                edu[i] = 4 #年輕進階是博士            else:                edu[i] = 3 #年老進階是碩士        else:            if age[i] < 40 :                edu[i] = 2 #年輕低級是大學            else:                edu[i] = 1 #年老低級是大專        #工資:規則計算 加上一點隨機變化  在加上一點入職時間長度的倍數鼓勵    sal = [0.] * num    for i in range(num):        sal[i] = round( ( 3000                           + yrs[i] * 200 + edu[i] * 1000 + ( lvl[i] - 1 ) * 3000 + sex[i] * 1000                           + i % 7 * 300 )                        * ( 1 + yrs[i] / 100 )                        ,3)    #離職風險:高低  #年輕大學生容易離職  #低學曆大年齡且入職時間短容易被淘汰    ris = [0] * num    for i in range(num):        if yrs[i] < 2 and age[i] < 35 and edu[i] == 2:            ris[i] = 1        if edu[i] == 1 and age[i] > 50 and yrs[i] < 5:            ris[i] = 1            df = pd.DataFrame({"sex":sex,                       "age":age,                       "lvl":lvl,                       "yrs":yrs,                       "edu":edu,                       "sal":sal,                       "ris":ris},                      index = emp)    return df # print(getEmpDataFrame(60))


Python練手,造樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.