Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groupby in Reverse

I have a pandas dataframe with name of variables, the values for each and the count (which shows the frequency of that row):

df = pd.DataFrame({'var':['A', 'B', 'C'], 'value':[10, 20, 30], 'count':[1,2,3]})

var  value  count
A    10     1
B    20     2
C    30     3

I want to use count to get an output like this:

var  value
A    10
B    20
B    20
C    30
C    30
C    30

What is the best way to do that?

like image 778
Rotail Avatar asked Sep 26 '20 14:09

Rotail


2 Answers

You can use index.repeat:

i = df.index.repeat(df['count'])
d = df.loc[i, :'value'].reset_index(drop=True)

   var  value
0   A     10
1   B     20
2   B     20
3   C     30
4   C     30
5   C     30
like image 85
Shubham Sharma Avatar answered Sep 19 '22 13:09

Shubham Sharma


Use repeat with reindex for this short one-liner:

df.reindex(df.index.repeat(df['count']))

Output:

  var  value  count
0   A     10      1
1   B     20      2
1   B     20      2
2   C     30      3
2   C     30      3
2   C     30      3

Or to eliminate the 'count' column:

df[['var','value']].reindex(df.index.repeat(df['count']))

OR

df.reindex(df.index.repeat(df['count'])).drop('count', axis=1)

Output:

  var  value
0   A     10
1   B     20
1   B     20
2   C     30
2   C     30
2   C     30
like image 23
Scott Boston Avatar answered Sep 17 '22 13:09

Scott Boston