How to repeat Pandas data frame?

pandas repeat value in column
pandas repeat rows based on column value
dataframe repeat rows
pandas replicate columns
pandas index repeat
pandas replicate rows n times
repeat column in pandas
repeat series to dataframe

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?

You can use the concat function:

In [13]: pd.concat([x]*5)
Out[13]: 
   a  b
0  1  2
0  1  2
0  1  2
0  1  2
0  1  2

If you only want to repeat the values and not the index, you can do:

In [14]: pd.concat([x]*5, ignore_index=True)
Out[14]: 
   a  b
0  1  2
1  1  2
2  1  2
3  1  2
4  1  2

pandas.Index.repeat, Repeat elements of a Index. Returns a new Index where each element of the current Index is repeated consecutively a given number of times. Parameters. repeats  Repeat or replicate the rows of dataframe in pandas python (create duplicate rows) Reverse the rows of the dataframe in pandas python; Reverse the column of the dataframe in pandas python; Reverse the string of column in pandas python; Square of the column in pandas python; Square root of the column in pandas python

I think it's cleaner/faster to use iloc nowadays:

In [11]: np.full(3, 0)
Out[11]: array([0, 0, 0])

In [12]: x.iloc[np.full(3, 0)]
Out[12]:
   a  b
0  1  2
0  1  2
0  1  2

More generally, you can use tile or repeat with arange:

In [21]: df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])

In [22]: df
Out[22]:
   A  B
0  1  2
1  3  4

In [23]: np.tile(np.arange(len(df)), 3)
Out[23]: array([0, 1, 0, 1, 0, 1])

In [24]: np.repeat(np.arange(len(df)), 3)
Out[24]: array([0, 0, 0, 1, 1, 1])

In [25]: df.iloc[np.tile(np.arange(len(df)), 3)]
Out[25]:
   A  B
0  1  2
1  3  4
0  1  2
1  3  4
0  1  2
1  3  4

In [26]: df.iloc[np.repeat(np.arange(len(df)), 3)]
Out[26]:
   A  B
0  1  2
0  1  2
0  1  2
1  3  4
1  3  4
1  3  4

Note: This will work with non-integer indexed DataFrames (and Series).

How to repeat Pandas data frame?, You can use the concat function: In [13]: pd.concat([x]*5) Out[13]: a b 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2. If you only want to repeat the values and not  Pandas Index.repeat() function repeat elements of an Index. The function returns a new index where each element of the current index is repeated consecutively a given number of times. The function returns a new index where each element of the current index is repeated consecutively a given number of times.

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

values = array(1, 2)
df2 = pd.DataFrame(index=arange(0,50),  columns=['a', 'b'])
df2[['a', 'b']] = values

will do the job. Perhaps you want to better explain what you're trying to achieve?

Repeat or replicate the rows of dataframe in pandas python (create , Repeat or replicate the rows of dataframe in pandas python (create duplicate rows) can be done in a roundabout way by using concat() function. example of .. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Append should work too:

In [589]: x = pd.DataFrame({'a':1,'b':2},index = range(1))

In [590]: x
Out[590]: 
   a  b
0  1  2

In [591]: x.append([x]*5, ignore_index=True) #Ignores the index as per your need
Out[591]: 
   a  b
0  1  2
1  1  2
2  1  2
3  1  2
4  1  2
5  1  2

In [592]: x.append([x]*5)
Out[592]: 
   a  b
0  1  2
0  1  2
0  1  2
0  1  2
0  1  2
0  1  2

Pandas Series: repeat() function, Returns a new Series where each element of the current Series is repeated consecutively a given number of times. Syntax: Series.repeat(self,  My pandas dataframe looks like this: Person ID ZipCode Gender 0 12345 882 38182 Female 1 32917 271 88172 Male 2 18273 552 90291 Female I want to replicate every ro

Try using numpy.repeat:

>>> df=pd.DataFrame(pd.np.repeat(x.values,5,axis=0),columns=x.columns)
>>> df
   a  b
0  1  2
1  1  2
2  1  2
3  1  2
4  1  2
>>> 

Python, The image of data frame before any operations is attached below. Example #1: Repeating same number of times. In this example, a single integer is passed as  pandas.Series.repeat¶ Series.repeat (self, repeats, axis=None) [source] ¶ Repeat elements of a Series. Returns a new Series where each element of the current Series is repeated consecutively a given number of times. Parameters repeats int or array of ints. The number of repetitions for each element. This should be a non-negative integer.

Python, Pandas Index.repeat() function repeat elements of an Index. Python | Pandas Series.sub() · Python | Pandas Series.mul() · Python | Pandas dataframe.sub()  Accessing pandas dataframe columns, rows, and cells At this point you know how to load CSV data in Python. In this lesson, you will learn how to access rows, columns, cells, and subsets of rows and columns from a pandas dataframe.

Different ways to iterate over rows in a Pandas Dataframe , a = np.repeat(1000, N) pd_dataset = pd.DataFrame({'a': a})# Goal is compute the column b where b(i) = a(i) + 1 b = a + 1. This can actually be  pandas.Index.repeat¶ Index.repeat (self, repeats, axis=None) [source] ¶ Repeat elements of a Index. Returns a new Index where each element of the current Index is repeated consecutively a given number of times. Parameters repeats int or array of ints. The number of repetitions for each element. This should be a non-negative integer.

How to Count Duplicates in Pandas DataFrame, In this guide, I'll show you how to count duplicates in Pandas DataFrame. I'll review 3 examples to demonstrate this concept in Python. Pivot tables¶ While pivot() provides general purpose pivoting with various data types (strings, numerics, etc.), pandas also provides pivot_table() for pivoting with aggregation of numeric data. The function pivot_table() can be used to create spreadsheet-style pivot tables.

Comments
  • I have a data frame which lacks one line for each identifier. I wanna insert this line in it, so what Im gonna do is to repeat this line for N times, and append it to the original data frame, then resort it.