How do I replicate rows a specific number of times according to a condition?

Patrick Shimabukuro

I'm trying to create a dataframe for a game simulation to calculate how many points each player would make according to a set of parameters.

I have this dataframe:

  PLAYER TYPE  Quantity in my base  STRENGTH  POWER  Number of Matches (min)  \
0           A                    2        15    200                        3   
1           B                    3        80     20                        0   

   Number of Matches (max)  
0                        5  
1                        2  

df

Each row in this df represents one type of player. On column "Quantity in my base" I have the number of times each type of player appears in my base and on columns "Number of Matches" the minimum and maximum number of matches they each type of player is expected to play in one day.

I need to replicate the rows for each type of players with their respective "Strength" and "Power" a number of times that is = to "Quantity in my base" times a random number between the min and max number of matches of each one. I'm doing this so that, on the new data frame, each row will represent one match per each specific player in my base.

For instance. If

    PLAYERTYPE Quantity_in_my_base Rand_Num_Matches Number_of_rows
0   A1  2   4   8
1   A2  3   3   9

Number of rows to be replicated

Than I want to create a second df like this:

PLAYERTYPE STRENGTH POWER
0   A   15  200
1   A   15  200
2   A   15  200
3   A   15  200
4   A   15  200
5   A   15  200
6   A   15  200
7   A   15  200
8   A   15  200
9   A   15  200
10  A   15  200
11  A   15  200
12  A   15  200
13  A   15  200
14  A   15  200
15  A   15  200
16  A   15  200

New df

But I want to do this for players A1, A2 and B1, B2, B3 and so on, in a way that each one will be replicating according to their respective random number.

Thank you so much!

Sy Ker

You could use .repeat() ;

repeat_df = df.loc[df.index.repeat(df['Number of Matches'])]
repeat_df[['PLAYERTYPE', 'STRENGTH', 'POWER']]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to replicate rows of a dataframe a fixed number of times?

Replicate X times specific rows of pandas dataframe

How do i subract a number a specific amount of times?

MySQL: How do a i insert a specific number of blank rows into a table

How do I calculate group subtotals for a specific number of rows in a table

How to replicate a list a certain number of times

How to replicate dataframe rows based on condition

How do I select rows in an Sqlite table with a limit on the number of rows with a condition?

Replicate multiple rows of events for specific IDs multiple times

Get Row Number according to a specific condition by sqlite

How can I count duplicate rows according to specific columns?

How do I return a specific number of rows (larger than 1), for each of the joined rows from the other table

How do i make a card game with Print statements that only allows each card to be used a specific number of times?

How do I count the number of times a specific time is between multiple time intervals (see example)

Split a dataframe according to priorities and specific number of rows

How do I replicate HttpRequest

Pandas : How can I assign group number according to specific value?

How to delete specific number of random rows in Pandas dataframe based on condition?

How to insert a value in a column's specific ranging rows, according to a condition. Pandas

In python, how do I make a matrix of the number of times(how many rows) each value in one column occurs with values in another column?

Repeat specific rows a given number of times in R

How do I find all rows in a table where the first column contains 1 (or a specific number) in bash?

How do I count the number of times a number appears in an array

How do I mark values ​in a series according to a specific rule?

How do I change a function according to a value in a specific column

Replicate the array number of times in Perl

How do I sort a character matrix according to a sorted number matrix?

How do I convert String to Number according to locale javascript

How can I make an NSTimer that repeats a specific number of times?