'how to generate n rows based on a value in a column in Big Query? [duplicate]

I have following table.

enter image description here

I need to transform this input as you can see in below output example:

enter image description here

If you have any ideas please share. Thank you very much!



Solution 1:[1]

Use Index.repeat for new rows by DataFrame.loc, remove no column, get flag columns and last create default RangeIndex:

df1 = (df.loc[df.index.repeat(df['no'])]
         .drop('no', axis=1)
         .assign(flag=1)
         .reset_index(drop=True))
print (df1)
   id_1  id_2  flag
0     A   100     1
1     A   100     1
2     A   100     1
3     A   200     1
4     A   301     1
5     A   301     1
6     B   122     1
7     B   122     1
8     B   122     1
9     B   122     1
10    B   100     1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jezrael