Home > other >  Concatenate data through for loop for several table
Concatenate data through for loop for several table

Time:06-14

I have four tables that I would like to make the same changes to, so I want to write a for loop to achieve it. I want to duplicate rows where the 'variable' column contains 'video' 3 times, and update the 'variable' column for each copy to be 'atv','ctc' and 'olv', then append them back to the original table.

Below is the script I write, if I do print(table_out.variable.unique()), I can see the appended variables. However, it does not overwrite the original tables. How should I modify the code so that outside the loop table_a,table_b,table_c,table_d would be updated?

Thank you very much!

table_list = [table_a,table_b,table_c,table_d]
table_output_list = [table_a,table_a,table_a,table_a
i=0

for table in table_list:
    for table_out in table_output_list:
        table_out = []
        
        atv = table[table['variable'].str.contains('video')].copy(deep=True)
        atv['variable'] = atv['variable'].replace('video', 'atv', regex=True)

        ctv = table[table['variable'].str.contains('video')].copy(deep=True)
        ctv['variable'] = ctv['variable'].replace('video', 'ctv', regex=True)

        olv= table[table['variable'].str.contains('video')].copy(deep=True)
        olv['variable'] = olv['variable'].replace('video', 'olv', regex=True)

        imps_list = [table, atv, ctv, olv]
        
        table_out = pandas.concat(imps_list)
        i = i 1

CodePudding user response:

In the first line of the second for loop, you set table_out = []. Does removing that line solve your issue?

CodePudding user response:

If you want to make the changes directly on the tables in table_list, then there's no need for the table_output_list since you can just iterate over the tables and in the last line, replace them with a

table_list = [table_a,table_b,table_c,table_d]

for i, table in enumerate(table_list):
   atv = table[table['variable'].str.contains('video')].copy(deep=True)
   atv['variable'] = atv['variable'].replace('video', 'atv', regex=True)

   ctv = table[table['variable'].str.contains('video')].copy(deep=True)
   ctv['variable'] = ctv['variable'].replace('video', 'ctv', regex=True)

   olv= table[table['variable'].str.contains('video')].copy(deep=True)
   olv['variable'] = olv['variable'].replace('video', 'olv', regex=True)

   imps_list = [table, atv, ctv, olv]
        
   table_list[i] = pandas.concat(imps_list)
  • Related