title: Using apply() rather than for loop - Pandas
body: <p>I am extracting maximum rainfall intensity for different durations using data with 5-minute rainfall totals. The code produces a list of max rainfall intensity for each duration (DURS). The code works but is slow when using data sets with 1,000,000+ rows. I am new to Pandas and I understand the <code>apply()</code> method is much faster than using a For loop but I do not know how to re-write a For loop using the <code>apply()</code> method.</p>
  <p>Example of dataframe:</p>
  <pre><code>                     Value[mm]  State of value
  Date_Time
  2020-01-01 00:00:00        1.0               5
  2020-01-01 00:05:00        0.5               5
  2020-01-01 00:10:00        4.0               5
  2020-01-01 00:15:00        2.0               5
  2020-01-01 00:20:00        2.0               5
  2020-01-01 00:25:00        0.5               5
  </code></pre>
  <p>Example of Code:</p>
  <pre><code>import matplotlib.pyplot as plt
  from matplotlib.ticker import ScalarFormatter
  import math, numpy, array, glob
  import pandas as pd
  import numpy as np
  
  pluvi_file = &quot;rain.csv&quot;                                          
  DURS = [5,6,10,15,20,25,30,45,60,90,120,180,270,360,540,720,1440,2880,4320] 
  
  df = pd.read_csv(pluvi_file, delimiter=',',parse_dates=[['Date','Time']])
  df['Date_Time'] = pd.to_datetime(df['Date_Time'], dayfirst=True)
  df.index = df['Date_Time']
  del df['Date_Time'] 
  
  lista = []
  for DUR in DURS:
      x = str(DUR)+' Min'
      df1 = df.groupby(pd.Grouper(freq=x)).sum()
      a = df1['Value[mm]'].max()/DUR*60 
      print(a)
      lista.append(a)
  </code></pre>
  <p>Output (Max rainfall intensity for each duration in mm/hr):</p>
  <pre><code>5 66.0
  6 60.0
  10 54.0
  15 40.0
  20 40.5
  25 30.0
  30 34.0
  45 26.666666666666664
  60 26.5
  90 20.666666666666668
  120 23.0
  180 12.166666666666666
  270 8.11111111111111
  360 9.416666666666666
  540 6.444444444444445
  720 4.708333333333333
  1440 3.8958333333333335
  2880 2.7708333333333335
  4320 2.1597222222222223
  </code></pre>
  <p>How would I re-write this using the <code>apply()</code> method?</p>