3
import numpy as np
from numpy import random
import pandas as pd
from pandas import Series, DataFrame

staff = ['Emelia','Raphael','Mike', 'Peter']

staff_l = []
for i in random.choice(staff, 4):
    staff_l.append(i)
s = Series(staff_l,index=['A','B','C','D'])

dframe = DataFrame(index=['A','B','C','D','E','F'],columns=['Morning','Afternoon'])

dframe.fillna(s)

I want to be able to fill the 'Morning' column with random choice of the names of staff until the column ends and fill the afternoon column also. Since I cant use a list I converted it into a series and still not working. When I use this code dframe.fillna(s) it gives me a NaN column. I cant seem to replace the NaN.

sambeth
  • 1,550
  • 2
  • 10
  • 18

2 Answers2

2

I think you need to be at least clear about the size of the the dataframe.

In your case

this one works

import pandas as pd
import random 
staff = ['Emelia','Raphael','Mike', 'Peter']
yourframe=pd.Dataframe(index=['A','B','C','D','E','F'],columns=['Morning','Afternoon'])
for x in yourframe.index.values:
    yourframe.set_value(x,'Morning',random.choice(staff))

for x in yourframe.index.values:
    yourframe.set_value(x,'Afternoon',random.choice(staff))

You can put them in the same loop since random.choice returns random values but i am not sure what you wanted.

Eliethesaiyan
  • 2,327
  • 1
  • 22
  • 35
0

fillna returns another dataframe. It does not actually modify dframe unless you add the inplace argument.

dframe.fillna(s, inplace=True)

Or alternatively, you can reassign dframe

dframe = dframe.fillna(s)

Whether or not you want to use inplace is up to you, but here is a decent discussion on the pros/cons of using it: guidelines on using pandas inplace keyword argument

Community
  • 1
  • 1
TheF1rstPancake
  • 2,318
  • 17
  • 17