1

I have a pandas dataframe consisting of 7 values:

   Minutiae         LR
0         1   1.975476
1         2   1.082983
2         3   0.269608
3         4   0.878350
4         5   2.820141
5         6   8.686183
6         7  24.340116
7         8  46.475523
8         9  66.139377

What I'm attempting to do is multiply each row a set amount of times, keeping both values the same. For example minutiae 3 by 1129 and 4 by 1085 etc.

So far I've only been able to find methods that increase the amount for each row but not individually.

I appreciate any insights to this, Thank you.

2
  • are you looking for df.loc[df.index.repeat(df['Minutiae'])] ? Commented Sep 27, 2019 at 8:27
  • can you give your expected output? Commented Sep 27, 2019 at 8:28

1 Answer 1

1

Create dictionary for number of repeats for each Minute, Series.map and then repeat index with Index.repeat, last use DataFrame.loc for repeat rows:

print (df)
   Minutiae        LR
0         1  1.975476
1         2  1.082983
2         3  0.269608
3         4  0.878350

d = {1:2, 2:1, 3:5, 4:3}

df1 = df.loc[df.index.repeat(df['Minutiae'].map(d))]
print (df1)
   Minutiae        LR
0         1  1.975476
0         1  1.975476
1         2  1.082983
2         3  0.269608
2         3  0.269608
2         3  0.269608
2         3  0.269608
2         3  0.269608
3         4  0.878350
3         4  0.878350
3         4  0.878350

Detail:

print (df['Minutiae'].map(d))
0    2
1    1
2    5
3    3
Name: Minutiae, dtype: int64

print (df.index.repeat(df['Minutiae'].map(d)))
Int64Index([0, 0, 1, 2, 2, 2, 2, 2, 3, 3, 3], dtype='int64')

Or create new column for repeating:

df['repeat'] = [2,1,5,3]
print (df)
   Minutiae        LR  repeat
0         1  1.975476       2
1         2  1.082983       1
2         3  0.269608       5
3         4  0.878350       3

df2 = df.loc[df.index.repeat(df['repeat'])]
print (df2)
   Minutiae        LR  repeat
0         1  1.975476       2
0         1  1.975476       2
1         2  1.082983       1
2         3  0.269608       5
2         3  0.269608       5
2         3  0.269608       5
2         3  0.269608       5
2         3  0.269608       5
3         4  0.878350       3
3         4  0.878350       3
3         4  0.878350       3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.