-4

i have two variable Total days and total present - so i need to create a new variable called percentage(total present/total days) and store the values in it.....like wise i needed to do for all the 50 values...i tried something like this

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

Can someone help me to understand on how to write a function

This is the data

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
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • what is the datatype of `Total days` and `Total present`? are they list or dataframe? – Sociopath Dec 20 '18 at 06:50
  • both are Int data types and its a data frame – Clive Robson Dec 20 '18 at 06:53
  • and what should be datatype of your expected output? is it pandas column or list? – Sociopath Dec 20 '18 at 07:00
  • [pandas-create-new-column-based-on-values-from-other-columns](https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns) – Patrick Artner Dec 20 '18 at 07:09
  • [make-new-column-in-panda-dataframe-by-adding-values-from-other-columns](https://stackoverflow.com/questions/34023918/make-new-column-in-panda-dataframe-by-adding-values-from-other-columns) – Patrick Artner Dec 20 '18 at 07:09
  • 1
    Questions that show no research effort at all - also no [mcve] are getting downvotet and closed. Please search SO for a solution before you post a question - chances are there are (several) solutions already provided. – Patrick Artner Dec 20 '18 at 07:12

1 Answers1

1

You can follow the steps either in Jupyter Notebook or in Python script:

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

Aminul
  • 1,427
  • 2
  • 8
  • 16