如何创建一个变量并将其值存储为50个观察值(python)?

时间:2018-12-20 06:43:22

标签: python python-3.x pandas

我有两个变量总天数和总天数-因此,我需要创建一个称为百分比(总天数/总天数)的新变量,并将值存储在其中。....就像我需要为所有50个值...我尝试过这样的事情

percentage = 0 
while percentage < 51:
    print(attend['Total present']/attend['Total days'])
    percentage = percentage + 1

有人可以帮助我了解如何编写函数

这是数据

Total days  Total present

90  79
90  69
90  78
90  66
90  83
90  72
90  79
90  65
90  75
90  84
90  80
90  69
90  80
90  83
90  65
90  74
90  75
90  82
90  82

1 个答案:

答案 0 :(得分:1)

您可以按照Jupyter Notebook或Python脚本中的步骤进行操作:

import pandas as pd
import numpy as np

n=0
attend=list()
while n < 51:
    arr = np.random.rand(1)
    attend.append([90,int(arr[0]*100)])
    n= n+1
df = pd.DataFrame(attend, columns=['Total days','Total present'])
df['percentage'] = (df['Total present']/ df['Total days'])*100

enter image description here

相关问题