Home > other >  Adding dataframe columns in nested for loop
Adding dataframe columns in nested for loop

Time:11-04

I recently asked this question: Add new columns to a dataframe in for loop but asked for the wrong thing.

I am able to write a for loop that adds a row to a dataframe each time as with the following example:

from random import randint
import numpy as np
dataframe = []
for i in range(2):
value = randint(0,10)
for j in range(2):
    mean = np.mean(value)
    dataframe.append(mean)
    cols=['mean']
    result=pd.DataFrame(dataframe, columns = cols)
result

This outputs a dataframe that looks like:

mean
8
8
9
9

I actually want all the values created from the j loop for each i to be in one column. For example, if when i is 0, the two values outputted from the j loop are 2 and 5, and if when i is 1, the two values outputted from the j loop are 4 and 7 then my dataframe would be:

mean_1  mean_2
2       4
5       7

CodePudding user response:

You could use string formatting in your column names? As you know the number of different values of i (in this case 2) you could predefine a dataframe and replace values as you go. Something like:

from random import randint
import numpy as np

nrows = 2
df = pd.DataFrame(index=range(nrows))

for i in range(2):
    df['mean_%i'%i] = 0
    value = randint(0,10)
    for j in range(2):
        mean = np.mean(value)
        df['mean_%i'%i].iloc[j] = mean

Or something similar.

  • Related