vp7
vp7

Reputation: 388

Generate data with python based on column data

I have a dataframe that looks like :

I_Code      Date_1      Date_2    s_count
FT-35447    01/09/2019  02/08/2019  6
FT-40664    01/09/2019  02/08/2019  6
FT-54185    01/09/2019  03/08/2019  3
FT-40664    01/09/2019  03/08/2019  3
FT-56984    02/09/2019  03/08/2019  3
FT-29238    02/09/2019  03/08/2019  3
FT-45919    02/09/2019  03/08/2019  3
FT-35447    01/09/2019  04/08/2019  2
FT-56984    02/09/2019  04/08/2019  2
FT-89801    02/09/2019  04/08/2019  2
FT-29238    02/09/2019  04/08/2019  2
FT-70293    03/09/2019  04/08/2019  2

I want to create a new dataframe that will have the same following fields along with a new field that would have a random number betweeen 1 to 100 but the count of rows would depend on s_count. for example, 1st entry would have 6 rows, 2nd one would have 6 rows, 3rd has 3 rows and so forth

Expected Output for row 1 in the df :

I_Code      Date_1      Date_2  s_count num
FT-35447    01/09/2019  02/08/2019  6   10
FT-35447    01/09/2019  02/08/2019  6   13
FT-35447    01/09/2019  02/08/2019  6   56
FT-35447    01/09/2019  02/08/2019  6   45
FT-35447    01/09/2019  02/08/2019  6   34
FT-35447    01/09/2019  02/08/2019  6   90

Is there a pandas way to achieve the same.

Thanks

Upvotes: 2

Views: 73

Answers (1)

jezrael
jezrael

Reputation: 863166

Use Index.repeat with DataFrame.loc for duplicated rows and then set new column values by numpy.random.randint:

df = df.loc[df.index.repeat(df['s_count'])].reset_index(drop=True)
df['num'] = np.random.randint(1, 100, size=len(df))

print (df.head(20))
      I_Code      Date_1      Date_2  s_count  num
0   FT-35447  01/09/2019  02/08/2019        6   83
1   FT-35447  01/09/2019  02/08/2019        6   84
2   FT-35447  01/09/2019  02/08/2019        6   11
3   FT-35447  01/09/2019  02/08/2019        6   83
4   FT-35447  01/09/2019  02/08/2019        6   90
5   FT-35447  01/09/2019  02/08/2019        6   12
6   FT-40664  01/09/2019  02/08/2019        6   33
7   FT-40664  01/09/2019  02/08/2019        6   69
8   FT-40664  01/09/2019  02/08/2019        6   11
9   FT-40664  01/09/2019  02/08/2019        6   29
10  FT-40664  01/09/2019  02/08/2019        6   46
11  FT-40664  01/09/2019  02/08/2019        6   44
12  FT-54185  01/09/2019  03/08/2019        3   92
13  FT-54185  01/09/2019  03/08/2019        3   46
14  FT-54185  01/09/2019  03/08/2019        3   45
15  FT-40664  01/09/2019  03/08/2019        3   87
16  FT-40664  01/09/2019  03/08/2019        3   88
17  FT-40664  01/09/2019  03/08/2019        3   78
18  FT-56984  02/09/2019  03/08/2019        3   89
19  FT-56984  02/09/2019  03/08/2019        3   18

Upvotes: 2

Related Questions