ENH: add new method `DataFrame.repeat`
See original GitHub issueIs 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:
- This is a DataFrame called
df
. value = df.values # change DataFrame to ndarray
- use
np.repeat
ornp.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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
-1 on adding even more api to a huge api
happy to take an example though for the doc string
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):Reindex does not reset the index, it just conforms it to the new index. It only works if the existing index is unique.