Search code examples
pythonpandasdatabasedataframeexpand

Expanding within the same df


I am looking for expanding my dataset based on any number e.g., (5)

I have the following data set

import pandas as pd
df = pd.DataFrame({"X": ["A", "B" ], "Y": [2, 1 ]})
print (df)

and I want to expand it by 5 and the ending dataset should look like the following

df = pd.DataFrame({"X": ["A", "A", "A", "A", "A", "B", "B", "B", "B", "B" ], "Y": [2, 2, 2, 2, 2, 1, 1, 1, 1, 1 ]})
print (df)

I have a very big dataset and would like expansion in a same dataframe instead of creating a new dataframe.

Thanks


Solution

  • You could use pd.concat()

    pd.concat([df]*5).sort_index()