Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to duplicate rows based on a counter column

Let's say I have a data frame called df

x count 
d 2
e 3
f 2

Count would be the counter column and the # times I want it to repeat.

How would I expand it to make it

x count
d 2
d 2
e 3
e 3
e 3
f 2
f 2

I've already tried numpy.repeat(df,df.iloc['count']) and it errors out

like image 732
Carol Zhou Avatar asked Jul 17 '15 22:07

Carol Zhou


1 Answers

You can use np.repeat()

import pandas as pd
import numpy as np

# your data
# ========================
df

   x  count
0  d      2
1  e      3
2  f      2

# processing
# ==================================
np.repeat(df.values, df['count'].values, axis=0)


array([['d', 2],
       ['d', 2],
       ['e', 3],
       ['e', 3],
       ['e', 3],
       ['f', 2],
       ['f', 2]], dtype=object)

pd.DataFrame(np.repeat(df.values, df['count'].values, axis=0), columns=['x', 'count'])

   x count
0  d     2
1  d     2
2  e     3
3  e     3
4  e     3
5  f     2
6  f     2
like image 155
Jianxun Li Avatar answered Sep 29 '22 17:09

Jianxun Li