I am trying to transform DataFrame, such that some of the rows will be replicated a given number of times. For example:
df = pd.DataFrame({\'class\': [\'A\',
repeated_items = [list(row[1]*row[2]) for row in df.itertuples()]
will create a nested list:
[['A'], [], ['C', 'C']]
which you can then iterate over with list comprehensions to create a new data frame:
new_df = pd.DataFrame({"class":[j for i in repeated_items for j in i]})
Of course, you can do it in a single line as well if you want:
new_df = pd.DataFrame({"class":[j for i in [list(row[1]*row[2]) for row in df.itertuples()] for j in i]})