Mark K
Mark K

Reputation: 9338

Pandas to create new rows from each exisitng rows

A short data frame and I want to create new rows from the existing rows.

What it does now is, each row, each column multiple a random number between 3 to 5:

import pandas as pd
import random

data = {'Price': [59,98,79],
'Stock': [53,60,60],
'Delivery': [11,7,6]}
df = pd.DataFrame(data)

for row in range(df.shape[0]):
    new_row = round(df.loc[row] * random.randint(3,5))
    new_row.name = 'new row'
    df = df.append([new_row])

print (df)



         Price  Stock  Delivery
0           59     53        11
1           98     60         7
2           79     60         6
new row    295    265        55
new row    294    180        21
new row    316    240        24

Is it possible that it can multiple different random numbers to each row? For example:

the 1st row 3 cells multiple (random) [3,4,5]
the 2nd row 3 cells multiple (random) [4,4,3] etc?

Upvotes: 2

Views: 140

Answers (3)

Bill Huang
Bill Huang

Reputation: 4648

You may also generate the multiplication coefficients with the same shape of df independently, and then concat the element-wise multiplied df * mul with the original df:

N.B. This method avoids the notoriously slow .append(). Benchmark: 10,000 rows finished almost instantly with this method, while .append() took 40 seconds!

import numpy as np
np.random.seed(111)  # reproducibility

mul = np.random.randint(3, 6, df.shape)  # 6 not inclusive
df_new = pd.concat([df, df * mul], axis=0).reset_index(drop=True)

Output:

print(df_new)
   Price  Stock  Delivery
0     59     53        11
1     98     60         7
2     79     60         6
3    177    159        33
4    294    300        28
5    395    300        30

print(mul)  # check the coefficients
array([[3, 3, 3],
       [3, 5, 4],
       [5, 5, 5]])

Upvotes: 1

Quang Hoang
Quang Hoang

Reputation: 150735

Use np.random.randint(3,6, size=3). Actually, you can do at once:

df * np.random.randint(3,6, size=df.shape)

Upvotes: 1

BENY
BENY

Reputation: 323226

Change the random to numpy random.choice in your for loop

np.random.choice(range(3,5),3)

Upvotes: 1

Related Questions