Skip to content Skip to sidebar Skip to footer

Repeating Counts In Pandas Data Frame

import pandas as pd df = pd.DataFrame({ 'item':['a','b','c','d','e','f','g','h','i','k'], 'counter':[1,2,3,1,2,3,1,2,3,1] }) Given this structure, what is the be

Solution 1:

By using np.put

a=df.index.values
a
Out[637]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64)
np.put(a,a,np.array([1,2,3]))
a
Out[639]: array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1], dtype=int64)
df['New']=a
df
Out[641]: 
   counter item  New11    a    122    b    233    c    311    d    122    e    233    f    311    g    122    h    233    i    311    k    1

Solution 2:

If performance is crucial, you may be able to make use of something like

np.repeat([[1, 2, 3]], len(df)/3 + 1, 0).ravel()

For a length 10^6 data frame, this is roughly 8 times faster to generate than the (much more elegant) df.index % 3.

Post a Comment for "Repeating Counts In Pandas Data Frame"