Pandas to create new rows from each exisitng rows

Mark K

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?

Thank you.

BEN_YO

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a new rows from column values of pandas data frame

From Dev

Pandas: Create new columns with values from other rows

From Java

How to create new rows in pandas by dictionary

From Dev

Using character value to create new rows in Pandas

From Dev

Pandas DataFrames: Create new rows with calculations across existing rows

From Dev

Python Pandas, one dict column, create new rows for each key/value pair

From Dev

How to create a new Pandas DataFrame from alternating boolean rows such that the new DataFrame is ready to plot?

From Java

Pandas way to create new dataframe from rows which contain list of dictionaries

From Dev

How to create new rows from values inside in a cloumn of pandas dataframe based on delimeter in Python?

From Dev

Based in a character, how to separate in new rows each cell of a pandas dataframe?

From Dev

pandas extract text between brackets and create rows for each bit of text

From Dev

Pandas: create rows for each unique value of a column, even with missing data

From Dev

create new column that compares across rows in pandas dataframe

From Dev

create new rows based specific condition and iterate over a list in pandas

From Dev

sort values and create new column based on rows Pandas

From Dev

Create a new column based by Iterating over consecutive rows in pandas

From Dev

Create new rows in Pandas, by adding to previous row, looping until x number of rows are made

From Dev

Create new CSV that excludes rows from old CSV

From Dev

Create a new regular table from visible and filtered rows of DataTable

From Dev

Create a new regular table from visible and filtered rows of DataTable

From Dev

R: create new dataframe rows are columns from another dataframe

From Dev

For loop to paste rows to create new dataframe from existing dataframe

From Dev

Create new columns based on rows

From Dev

Create new column with multiple rows

From Dev

Pandas calculate a new column from multiple other columns and subset of rows

From Dev

Pandas write variable number of new rows from list in Series

From Dev

Copy certain rows from pandas dataframe to a new one (Time condition)

From Dev

Insert new rows in pandas dataframe

From Dev

Pandas new dataframe by rolling the rows

Related Related

  1. 1

    How to create a new rows from column values of pandas data frame

  2. 2

    Pandas: Create new columns with values from other rows

  3. 3

    How to create new rows in pandas by dictionary

  4. 4

    Using character value to create new rows in Pandas

  5. 5

    Pandas DataFrames: Create new rows with calculations across existing rows

  6. 6

    Python Pandas, one dict column, create new rows for each key/value pair

  7. 7

    How to create a new Pandas DataFrame from alternating boolean rows such that the new DataFrame is ready to plot?

  8. 8

    Pandas way to create new dataframe from rows which contain list of dictionaries

  9. 9

    How to create new rows from values inside in a cloumn of pandas dataframe based on delimeter in Python?

  10. 10

    Based in a character, how to separate in new rows each cell of a pandas dataframe?

  11. 11

    pandas extract text between brackets and create rows for each bit of text

  12. 12

    Pandas: create rows for each unique value of a column, even with missing data

  13. 13

    create new column that compares across rows in pandas dataframe

  14. 14

    create new rows based specific condition and iterate over a list in pandas

  15. 15

    sort values and create new column based on rows Pandas

  16. 16

    Create a new column based by Iterating over consecutive rows in pandas

  17. 17

    Create new rows in Pandas, by adding to previous row, looping until x number of rows are made

  18. 18

    Create new CSV that excludes rows from old CSV

  19. 19

    Create a new regular table from visible and filtered rows of DataTable

  20. 20

    Create a new regular table from visible and filtered rows of DataTable

  21. 21

    R: create new dataframe rows are columns from another dataframe

  22. 22

    For loop to paste rows to create new dataframe from existing dataframe

  23. 23

    Create new columns based on rows

  24. 24

    Create new column with multiple rows

  25. 25

    Pandas calculate a new column from multiple other columns and subset of rows

  26. 26

    Pandas write variable number of new rows from list in Series

  27. 27

    Copy certain rows from pandas dataframe to a new one (Time condition)

  28. 28

    Insert new rows in pandas dataframe

  29. 29

    Pandas new dataframe by rolling the rows

HotTag

Archive