question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: add new method `DataFrame.repeat`

See original GitHub issue

Is your feature request related to a problem?

I sometimes would generate some faker but having rule data. Just like in a (2d) data, repeat some rows or columns serial times.

The steps I do:

  1. This is a DataFrame called df.
  2. value = df.values # change DataFrame to ndarray
  3. use np.repeat or np.tile method to repeat.

Describe the solution you’d like

I thought why not add a method repeat to DataFrame, since Series has already owned the repeat method.

About the DataFrame.repeat parameters

Similar to numpy.repeat(a, repeats, axis=None)

def repeat(
    self,
    repeats: int | list[int],
    axis: int | str = 0,
) -> pd.DataFrame | None:
    ...

DataFrame is a 2d data structure, so it is hard to repeat each element. And the aim of DataFrame.repeat is to repeat one row/column or some rows/columns.

So the axis argument must be specific to 0 or 1.

And if the repeats is a single value int, that means repeat all row/column repeats times.

Examples of this method

>>> 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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jrebackcommented, Aug 9, 2021

-1 on adding even more api to a huge api

happy to take an example though for the doc string

1reaction
samukwekucommented, Aug 9, 2021

I’m not sure if it makes sense to have duplicate columns, but this is how you can repeat a dataframe with the index(or columns, which is an index too):

df.reindex(df.index.repeat(2))  # repeat on the index

   a  b
0  1  3
0  1  3
1  2  4
1  2  4


df.reindex(columns=df.columns.repeat(2)) # repeat on the columns

   a  a  b  b
0  1  1  3  3
1  2  2  4  4


 df.reindex(columns=df.columns.repeat([1,2])) # repeat column a once, b twice

   a  b  b
0  1  3  3
1  2  4  4

Reindex does not reset the index, it just conforms it to the new index. It only works if the existing index is unique.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ENH: pandas.rep() function similar to R rep() or numpy.repeat ...
You can create a repeating series using pandas.Series(x,index=range(50)) but it does not work for all object types. If a list, dictionary, or ...
Read more >
How to repeat a Pandas DataFrame? - python - Stack Overflow
I wanna insert this line in it, so what Im gonna do is to repeat this line for N times, and append it...
Read more >
pandas.Series.repeat — pandas 1.5.2 documentation
Returns a new Series where each element of the current Series is repeated consecutively a given number of times. Parameters. repeatsint or array...
Read more >
functools — Higher-order functions and operations on callable ...
Simple lightweight unbounded function cache. Sometimes called “memoize”. Returns the same as lru_cache(maxsize=None) , creating a thin wrapper around a ...
Read more >
Python enumerate(): Simplify Looping With Counters
When you use enumerate() , the function gives you back two loop variables: The count of the current iteration; The value of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found