This is my data frame that should be repeated for 5 times:
>>> x = pd.DataFrame({'a':1,'b':2},index = range(1))
>>> x
a b
0 1 2
I wanna have the result like this:
>>> x.append(x).append(x).append(x)
a b
0 1 2
0 1 2
0 1 2
0 1 2
But there must be a way smarter than keep appending.. Actually the data frame Im working on should be repeated for 50 times..
I haven't found anything practical, including those like np.repeat
---- it just doesnt work on data frame.
Could anyone help?
Try using
numpy.repeat
:Append should work too:
You can use the
concat
function:If you only want to repeat the values and not the index, you can do:
I would generally not repeat and/or append, unless your problem really makes it necessary - it is highly inefficiently and typically comes from not understanding the proper way to attack a problem.
I don't know your exact use case, but if you have your values stored as
will do the job. Perhaps you want to better explain what you're trying to achieve?
I think it's cleaner/faster to use
iloc
nowadays:More generally, you can use
tile
orrepeat
witharange
:Note: This will work with non-integer indexed DataFrames (and Series).