I wish to create a dataframe with random values, which are either random numbers or randomly selected from a list. When I run this code something weird happens, while some of the columns in the dataframe like 'sector', 'firm_id', 'sector_choice', give expected outputs some don't. All the columns which have values that are list (a) have the same exact value in all the rows. I have tried to introduce the new random seed but that is not changing the output. Please help me figure out what's going wrong.
import random
import pandas as pd
a=['1006', '1014', '1042', '1051', '1058', '1063', '1066', '1076', '1084', '1085', '1087', '1094', '1107', '1120', '1130', '1132', '1139', '1140', '1144', '1159', '1161', '1169', '2004', '2008', '2009', '2010', '2011', '2014', '2015', '2018']
sectors=['agri', 'auto', 'build', 'cater', 'clean', 'cons', 'elec', 'fin', 'food', 'fuel', 'furn', 'indus', 'it', 'med', 'off', 'pr', 'print', 'read', 'rev', 'safe', 'tex', 'transp', 'travel', 'waste', 'water']
columns=['firm_id1','sector_choice','national_pde_authority_choice','national_pde_commission_choice','national_pde_hospital_choice','national_pde_university_choice' ,'national_pde_other_choice', 'local_pde_central_choice', 'local_pde_western_choice','local_pde_northern_choice' ,'local_pde_eastern_choice','subjects rejected']
firmchoices_d=pd.DataFrame(columns=columns)
#random.seed(5) #Doing this does not have an effect, nor does using system random or time
firmchoices_d = firmchoices_d.append(pd.Series(), ignore_index=True)
for i in range(2000):
firmchoices_d['firm_id1'].loc[i]=random.randrange(1000,2000, 2)
firmchoices_d['subjects rejected'].loc[i]=random.randrange(1,1000, 1)
firmchoices_d['sector_choice'].loc[i]=str(random.choices(sectors,k=random.randrange(1,4,1))).strip('[]')
firmchoices_d['national_pde_authority_choice']=str(random.choices(a,k=random.randrange(1,2,1))).strip('[]')
firmchoices_d['national_pde_commission_choice']=str(random.choices(a,k=random.randrange(1,3,1))).strip('[]')
firmchoices_d['national_pde_hospital_choice']=str(random.choices(a,k=random.randrange(1,3,1))).strip('[]')
firmchoices_d['national_pde_university_choice']=str(random.choices(a,k=random.randrange(1,2,1))).strip('[]')
firmchoices_d['national_pde_other_choice']=str(random.choices(a,k=random.randrange(1,3,1))).strip('[]')
firmchoices_d['local_pde_central_choice']=str(random.choices(a,k=random.randrange(1,3,1))).strip('[]')
firmchoices_d['local_pde_western_choice']=str(random.choices(a,k=random.randrange(1,2,1))).strip('[]')
firmchoices_d['local_pde_northern_choice']=str(random.choices(a,k=random.randrange(1,4,1))).strip('[]')
firmchoices_d['local_pde_eastern_choice']=str(random.choices(a,k=random.randrange(1,3,1))).strip('[]')
firmchoices_d = firmchoices_d.append(pd.Series(), ignore_index=True)
Aucun commentaire:
Enregistrer un commentaire