title: Apache Spark - Python - How to use range function in Pyspark
body: <p>
  I have some lines of space seperated input data:</p>
  
  <pre class="lang-python prettyprint-override"><code>Name Company Start_Date End_Date 
  Naresh HDFC 2017-01-01 2017-03-31
  Anoop ICICI 2017-05-01 2017-07-30
  </code></pre>
  
  <p>I need output as :</p>
  
  <pre class="lang-python prettyprint-override"><code>Naresh HDFC 2017 01
  Naresh HDFC 2017 02
  Naresh HDFC 2017 03
  Anoop ICICI 2017 05
  Anoop ICICI 2017 06
  Anoop ICICI 2017 07 
  </code></pre>
  
  <p>I have made a textfile of this data and placed this on my Hadoop cluster, and I have written the code but I am having some problem in getting the output. Kindly help.
  I am not getting how to extract month from the entries and put them in range function, so I have hard coded in range function a value of 3.
  <br>Code:</p>
  
  <pre class="lang-python prettyprint-override"><code>from pyspark import SparkConf,SparkContext
  from pyspark.sql import SQLContext,Row
  from pyspark.sql.types import *
  import datetime
  sc = SparkContext()
  sqlcon = SQLContext(sc)
  
  month_map={'01':1,'02':2,'03':3,'04':4,'05':5,'06':6,'07':7,'08':8,'09':9,
  '10':10,'11':11,'12':12}
  
  def get_month(str):
      return datetime.date(int(str[:4]),month_map[str[5:7]],int(str[8:10]))
  
  def parse_line(str):
      match = str.split()
      return (Row(name = match[0],type = match[1],start_date = 
      get_month(match[2]),end_date = get_month(match[3])))
  
  
  #-----------------create RDD---------------
  
  filepath = '/user/vikasmittal/Innovacer_data.txt'
  rdd1 = sc.textFile(filepath)
  rdd2 =rdd1.map(parse_line)
  for i in range(3):
      rdd3 = rdd2.map(lambda l:(l.name,l.type,l.start_date.year,i))
      print(rdd3.collect())
  </code></pre>