dtoolkit.accessor.dataframe.repeat#

dtoolkit.accessor.dataframe.repeat(df: DataFrame, repeats: int | Hashable | list[int], /, axis: Literal[0, 1, 'index', 'columns'] = 0) DataFrame[source]#

Repeat row or column of a DataFrame.

Returns a new DataFrame where each row/column is repeated consecutively a given number of times.

A sugary syntax wraps numpy.repeat().

Parameters:
repeatsint, Hashable or array of ints

The number of repetitions for each element. This should be a non-negative integer. Repeating 0 times will return an empty DataFrame. The order of priority type is int > Hashable.

  • int : the row or column will be repeated repeats times.

  • array of int : the row or column at the i-th position will be repeated. Its length must be the same as the axis being repeated.

  • Hahsable : the row or column will be repeated by with the given column.

axis{0 or ‘index’, 1 or ‘columns’}, default 0

The axis along which to repeat.

  • 0, or ‘index’ : Along the row to repeat.

  • 1, or ‘columns’ : Along the column to repeat.

Returns:
DataFrame

Newly created DataFrame with repeated elements.

See also

numpy.repeat

This transformer’s prototype method.

Examples

>>> import pandas as pd
>>> import dtoolkit
>>> df = pd.DataFrame({'a': [1, 2], 'b':[3, 4]})
>>> df
   a  b
0  1  3
1  2  4

Each row repeat two times.

>>> df.repeat(2)
   a  b
0  1  3
0  1  3
1  2  4
1  2  4

Each column repeat two times.

>>> df.repeat(2, 1)
   a  a  b  b
0  1  1  3  3
1  2  2  4  4

a column repeat 1 times, b column repeat 2 times.

>>> df.repeat([1, 2], 1)
   a  b  b
0  1  3  3
1  2  4  4

Use the ‘a’ column to repeat the row.

>>> df.repeat('a')
   a  b
0  1  3
1  2  4
1  2  4