blueorchid
blueorchid

Reputation: 99

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

Input:

mp = [1,2,3,4]
tw = [4,7,3,5]
cw = []

# create the data frame:

df = pd.DataFrame((cw),
    columns = ["1","2","3","4"])

# add list tw to the first row

df.loc[len(df)] = tw
df

Output:

    1      2      3      4
0   4      7      3      5

Desired Output:

      1      2      3      4
0     4      7      3      5
1     5      8      4      6
2     6      9      5      7
...  ...    ...    ...    ...
3000 3003   3006   3002   3004

Starting from an initial reference row of list "tw" I need to add 1 to the starting value and keep going. How do I loop and keep creating rows so that the next row is the previous row +1, I need to do this for 3000 rows. A lot of solutions I've seen require me to create lists and add to the pandas dataframe however I cannot manually create 3000 lists and then manually add them to my dataframe. There must be a way to loop over this, please help!

Upvotes: 2

Views: 1645

Answers (4)

tw = [4,7,3,5]

cw = []
df = pd.DataFrame((cw),
    columns = ["1","2","3","4"])
df.loc[len(df)] = tw
df.reset_index()

for key in range(2999):
    row = df.loc[key].values #convert to array
    row = row.tolist() #convert to nested list
    row=[x+1 for x in row]
    df.loc[key+1]=row
    print (row)

Upvotes: 1

ALollz
ALollz

Reputation: 59549

I would use numpy for this, that way we can perform a simple addition with braodcasting. Then create the DataFrame after you've made the array. Growing a DataFrame row by row is horribly inefficient.

import pandas as pd
import numpy as np

N = 3000           # Number of rows
tw = [4, 7, 3, 5]  # Initial column values

df = pd.DataFrame(np.array(tw) + np.arange(N)[:, None],
                  columns=[1,2,3,4])
#         1     2     3     4
#0        4     7     3     5
#1        5     8     4     6
#2        6     9     5     7
#...    ...   ...   ...   ...
#2997  3001  3004  3000  3002
#2998  3002  3005  3001  3003
#2999  3003  3006  3002  3004
# 
#[3000 rows x 4 columns]

Another option is to create the one row DataFrame, reindex to create all the rows, fill with the value you want to add (1) and cumsum.

df = (pd.DataFrame([tw], columns=[1,2,3,4])
        .reindex(range(N))
        .fillna(1, downcast='infer')
        .cumsum())

Upvotes: 1

Guillermo Garcia
Guillermo Garcia

Reputation: 573

Here is a customizable function you could use to select the first row and the number of rows that you want to add to your panda:

import pandas as pd
def expandingRow(df, firstRow, nRows):
    for i in range (0,nRows):
       df.loc[len(df)] = [x+len(df.index) for x in firstRow]

mp = [1,2,3,4]
tw = [4,7,3,5]
cw = []

# create the data frame:

df = pd.DataFrame((cw),
    columns = ["1","2","3","4"])

expandingRow(df, tw, 3000)
df
Out[1]: 
         1     2     3     4
0        4     7     3     5
1        5     8     4     6
2        6     9     5     7
3        7    10     6     8
4        8    11     7     9
   ...   ...   ...   ...
2995  2999  3002  2998  3000
2996  3000  3003  2999  3001
2997  3001  3004  3000  3002
2998  3002  3005  3001  3003
2999  3003  3006  3002  3004

[3000 rows x 4 columns]

Upvotes: 1

Mayank Porwal
Mayank Porwal

Reputation: 34056

Use df.append with df.tail and df.add:

n = 3000 # this is the value of total rows you want

In [209]: for i in range(n):
     ...:     df = df.append(df.tail(1).add(1))
     ...: 

Example:

In [214]: df
Out[214]: 
   1  2  3  4
0  4  7  3  5

In [215]: n = 5

In [216]: for i in range(5):
     ...:     df = df.append(df.tail(1).add(1))
     ...: 

In [217]: df
Out[217]: 
   1   2  3   4
0  4   7  3   5
0  5   8  4   6
0  6   9  5   7
0  7  10  6   8
0  8  11  7   9
0  9  12  8  10

Upvotes: 1

Related Questions