0

I have a df

    1   1   2   2
    2   2   1   1

I have written a function which:

  • takes the df in a for loop,
  • adds row(s) with a default value
  • replaces the values with another value in randomly selected cols
  • writes to csv

This is my code:

def add_x(df, max):
    gt_w_x = df.copy()
    counter = 0


    for i in range(1, max):
        if len(gt_w_x) != max:
            counter+=1
            # add new row with default value
            gt_w_x.loc[-1,:] = 1
        
            # reset index
            gt_w_x = gt_w_x.reset_index(drop=True)
        
            # how to loop over these values for x ??
            x = 1
            #x = 2
        
            # assign value 'X' to x randomly selected cols on last row 
            gt_w_x.iloc[-1:, random.sample(list(range(gt_w_x.shape[1])), x)] = 'X'
        
            x = str(x)
            n = str(counter)
        
            # write to file
            df_path = 'test/' + x + '_' + n + '.csv'
            gt_w_x.to_csv(df_path) 

max = 4
add_x(df, max)

The output on my system is

test/1_1.csv  
test/1_2.csv  


cat test/1_1.csv    
0,1.0,1.0,2.0,2.0  
1,2.0,2.0,1.0,1.0  
2,1.0,X,1.0,1.0  


cat test/1_2.csv  
0,1.0,1.0,2.0,2.0  
1,2.0,2.0,1.0,1.0  
2,1.0,X,1.0,1.0  
3,1.0,X,1.0,1.0  

How do I loop over values for x? The desired output for x = 1 and x = 2 is

test/1_1.csv  
test/1_2.csv  
test/2_1.csv    
test/2_2.csv  

Currently, I run the function by commenting out different values for x which is suboptimal.

2
  • Use another for loop for the different x values. Commented Nov 10, 2022 at 13:50
  • I don't understand why there is a question here. "How do I loop over values for x?" Well, do you see how the code already loops over values for i? Did you try doing the same thing? Commented Nov 12, 2022 at 1:15

1 Answer 1

1

You can use a nested for loop. It works just like the one you have at the beginning of the function:

def add_x(df, max):
    for x in range(1,3):
        gt_w_x = df.copy()
        counter = 0

        for i in range(1, max):
            if len(gt_w_x) != max:
                counter+=1
                # add new row with default value
                gt_w_x.loc[-1,:] = 1

                # reset index
                gt_w_x = gt_w_x.reset_index(drop=True)

                # assign value 'X' to x randomly selected cols on last row
                gt_w_x.iloc[-1:, random.sample(list(range(gt_w_x.shape[1])), x)] = 'X'

                n = str(counter)

                # write to file
                df_path = 'test/' + str(x) + '_' + n + '.csv'
                gt_w_x.to_csv(df_path)

max = 4
add_x(df, max)
Sign up to request clarification or add additional context in comments.

2 Comments

This does not appear to give the right output. In the file test/1_2.csv there are two rows containing X, which is correct. However, the first of these X-containing rows has X in three columns and the second X-containing row has X in one column. Maybe I'm missing something. Would you be willing to check?
Hi @peter_parker, I updated the looping order for x and inlined the relevant str(x) function so it won't change x mid-loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.