title: How to replace the current cell value with a value that is maximum of the current and previous cell in a column in a pandas dataframe in Python
body: <p>I have a large DataFrame, and want to run the following code (which is correct) much faster to reduce the processing time.
  Logic: I want to sort the values in every column in increasing (cumulative) order, but when a 'zero row' (ex: 5th row) is encountered, that row is to be skipped (code: if row2 == 0: continue). If the 2nd cell (row2) value is lower than the previous one (row1), then 2nd cell value is replaced by the first one. I want to make this code run faster. If the below code is used, then iterating through df having a shape of (1305 cols, 3000 rows) takes around 1.5 minutes for completion. Need help in this regard to reduce the processing time.</p>
  <pre class="lang-py prettyprint-override"><code>import pandas as pd
  
  df = pd.DataFrame({
      'A1': [118.0, 189.0, 154.0, 129.0, 0, 227.0, 208.0, 200.0, 733.0, 804.0,
             861.0],
      'A2': [108.0, 54.0, 122.0, 148.0, 0, 0, 0, 0, 838.0, 996.0, 987.0],
      'A3': [70.0, 89.0, 107.0, 148.0, 0, 149.0, 241.0, 222.0, 825.0, 954.0,
             1138.0],
      'A4': [80.0, 40, 117.0, 158.0, 0, 0, 0, 0, 700, 899, 850]
  })
  
  for i in df.columns:
      for j in range(len(df) - 1):
          row1 = df[i][j]
          row2 = df[i][j + 1]
          # print(row1, row2)
          if row2 == 0:
              continue
          elif row2 - row1 &lt; 0:
              # print('true')
              df[i][j + 1] = df[i][j]
  </code></pre>