vendredi 8 octobre 2021

Is there faster way than iterrows to iterate over the rows and update the value if the condition is met for my specific problem?

Here is the code we have tried:

import pandas as pd

df = pd.read_csv('tryout.hmo', header = None, delimiter=r'\s+', skiprows=[i for i in range(52363)])
df2 = pd.read_csv('Density.topo', header= None, delimiter=r'\s+', skiprows=2)

for idx, row in df.iterrows():
   for idx2, row2 in df2.iterrows():
      if row[0] == row2[0]:
          if row2[2] == 1.0 and row2[3] == 1e-5 :
            # it is a magnet, value 18 is assigned to an element
              row[1] = 18
          elif row2[2] == 1.0 and row2[3] == 1.0 :
            # it is iron, value 19
              row[1] = 19
          else:
            # it is air, value 20
            row[1] = 20
df.to_csv('new_tryout.csv')

First file (tryout) has data in BEG_ELEM_DATA for different elements (in last 2 rows, value 10 in column 2 comes after element 3749, 3949 and so on)

        BEG_ELEM_DATA
        -------        0        0        0     3156        0    15208        0        0        
     
         1    1 106     1177     1200     1184     1201     1183     1202
          ...
      3749   10 106    10690    10692    10693    10695    10658    10689
      3949   10 106    11201    11223    11224    11226    11171    11202

and second file has densities, based on these densities, the value 10 changes to 18 or 19 or 20. The rule is if density_1 = 1 & density_2 = 0, then its a magnet, if density_1 = 1 & density_2 = 1, then its a iron otherwise its air for density_1 = 0 & density_2 = 0. Density file is as follows:

 $ON
 $DVAR
  3749   2   1.0   1e-5
  3949   2   1.0   1e-5
  ..
  ..

Final file will be same entire tryout file but in BEG_ELEM_DATA, the values will be changed to 18/19/20 if the condition is met. With above code, not only it is so slow with iterrows but also not giving the required output. I could not figure out df.apply() or itertuple method for the if conditions. I attached the files in the link. tryout.hmo and density.hmo files Your help is much appreciated thanks!

Aucun commentaire:

Enregistrer un commentaire