python加权随机_python根据excel的一列数据产生加权随机数

需求

最近遇到一个奇葩的事,行政那边说,让估算一下明年的这些杂七杂八费,然后给了我一个excel,里面有200多个这样的费用。我没做过行政,也搞不清这个到底咋来,为什么要弄这玩意。一番交流,原来是上头要的,不一定看,但是东西得有,让我弄个数字和去年差不多的就行。于是变有了下面的故事

设计

1.需要读取excel. 用的库是xlwing。可以根据sheet名称定位sheet,然后用range()方法定位一行,一列,或者指定几个单元格。

例如:定位 A1到A3 ,range1=sheet.range(“A1:A3”)

具体用法很多,在xlwing的官网上有介绍。http://docs.xlwings.org/en/stable/api.html

2.需要产生随机数,用的库是numpy,产生一个加权的随机数,然后我用的加权随机数规则为:

【-10%,概率10%】【不变,概率30%】【+2%,概率30%】【+5%,概率20%】【+10%,概率10%】

代码实现

代码中分为两个function,一个是读取excel,一个是根据一个值,产生一个加权随机数。观众老爷可以参考,对有疑问的地方,可以再来与我沟通O(∩_∩)O

Python

# -*- coding: UTF-8 -*-

import xlwings

import numpy as np

def import_excel(filename, sheet_name, source,target):

'''

导入一个excel,根据某一列的值,产生一个加权随机数,覆盖掉另一列的值

:param filename: excel路径

:param sheet_name: sheet的名字

:param source: 源列的第一个值 如:A1,C1

:param target: 目标列的第一个值,如B1,D2

:return:

'''

app = xlwings.App(visible=False, add_book=True)

app.display_alerts = False

app.screen_updating = False

wb = app.books.open(filename)

sht = wb.sheets[sheet_name]

rng1 = sht.range(source)

rng2 = sht.range(source).end('down')

rng3 = sht.range(rng1, rng2)

all_rng = rng3.value

list_dest = []

for item in all_rng:

dest_item = arithmetic(item)

# 这里要把结果变成[[1],[2]]这样的形式,才是一列的数据

# 如果是[1,2]这样,是一行的数据

list_temp = []

list_temp.append(dest_item)

list_dest.append(list_temp)

sht.range(target).value = list_dest

wb.save(filename)

wb.close()

app.quit()

def arithmetic(source):

'''

加权随机算法

:param source: 输入一个原值

:return: 返回浮动后的值

'''

power = np.array([0.1, 0.3, 0.3, 0.2, 0.1])

index = np.random.choice([-0.1, 0, 2, 5, 10], p=power.ravel())

rand = source * index / 100

dest = int(source + rand)

return dest

if __name__ == '__main__':

import_excel("Book1.xlsx", 'Sheet1','C2','D2')

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

# -*- coding: UTF-8 -*-

importxlwings

importnumpyasnp

defimport_excel(filename,sheet_name,source,target):

'''

导入一个excel,根据某一列的值,产生一个加权随机数,覆盖掉另一列的值

:param filename: excel路径

:param sheet_name: sheet的名字

:param source: 源列的第一个值 如:A1,C1

:param target: 目标列的第一个值,如B1,D2

:return:

'''

app=xlwings.App(visible=False,add_book=True)

app.display_alerts=False

app.screen_updating=False

wb=app.books.open(filename)

sht=wb.sheets[sheet_name]

rng1=sht.range(source)

rng2=sht.range(source).end('down')

rng3=sht.range(rng1,rng2)

all_rng=rng3.value

list_dest=[]

foriteminall_rng:

dest_item=arithmetic(item)

# 这里要把结果变成[[1],[2]]这样的形式,才是一列的数据

# 如果是[1,2]这样,是一行的数据

list_temp=[]

list_temp.append(dest_item)

list_dest.append(list_temp)

sht.range(target).value=list_dest

wb.save(filename)

wb.close()

app.quit()

defarithmetic(source):

'''

加权随机算法

:param source: 输入一个原值

:return: 返回浮动后的值

'''

power=np.array([0.1,0.3,0.3,0.2,0.1])

index=np.random.choice([-0.1,0,2,5,10],p=power.ravel())

rand=source*index/100

dest=int(source+rand)

returndest

if__name__=='__main__':

import_excel("Book1.xlsx",'Sheet1','C2','D2')

运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值