Home > Enterprise >  Expanding within the same df
Expanding within the same df

Time:08-11

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

CodePudding user response:

Let us do

out = df.reindex(df.index.repeat(5)).reset_index(drop=True)
Out[834]: 
   X  Y
0  A  2
1  A  2
2  A  2
3  A  2
4  A  2
5  B  1
6  B  1
7  B  1
8  B  1
9  B  1

CodePudding user response:

Use np.repeat

out = df.apply(lambda x: np.repeat(x,5)).reset_index(drop=True)

print(out):

   X  Y
0  A  2
1  A  2
2  A  2
3  A  2
4  A  2
5  B  1
6  B  1
7  B  1
8  B  1
9  B  1

CodePudding user response:

You could use pd.concat()

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