title: How to get index of all specified columns from csv file depending upon the name of the columns using python
body: <p>I have some few csv files... which looks something like this...</p>
  <pre><code>id,exam1,exam1,exam1,exam2,exam2,exam2,exam3,exam3,exam3....
  lv-1,sub1,sub2,sub3,sub1,sub2,sub3,sub1,sub2,sub3....
  </code></pre>
  <p>in the next line Here I have data.... id number and respective marks...</p>
  <p>The problem is when I use python and try to get the index value of the column I get only the index of the first occurrence of column name.</p>
  <p>example... Here is my python code.</p>
  <pre class="lang-py prettyprint-override"><code>    import csv
      file = [location of the file]
      
      data = csv.reader(open(file,'r'),delimiter=',')
      header = next(data) 
      header = next(data)
  
      i = header.index(&quot;sub2&quot;)
      j = header.index(&quot;sub3&quot;)
      
      print(i,j)
  </code></pre>
  <p>The output gives - 2,3
  But I want index numbers of other sub2 and sub3 also.</p>
  <p>I thought to iterate through the 2nd row but no idea how to accomplish it and get the index value.</p>
  <p>if anyone can help me please tell me how to do it with python code ?</p>
  <p>&lt;------------------------------------------------------SOLUTION--------------------------------------------------&gt;</p>
  <pre class="lang-py prettyprint-override"><code>    import csv
  
      def get_index(row_line):
          j = 0
          flag = 0
          while flag!=-1:
              item = row_line[j]
              if (item == 'sub2' or item == 'sub3'):
                  indexes.append(j)
              j = j+1
              if(j &gt;= len(row_line)):
                  flag = -1
          return indexes
  
      s_file = input_file.csv
  
      data = csv.reader(open(s_file,'r'),delimiter=',')
      header = next(data) # returns the headers or `None` if the input is empty
      header = next(data) # As I have sub2,sub3 in the second row so moving to 2nd row
  
      with open(s_file, 'r') as fin:
          reader = csv.reader(fin)
  
          indexes = get_index(header)
          print(indexes) #Here you get the index values of all sub2 and sub3...
      
      # After you get the index you can carry-on with other editing on the csv file
  </code></pre>
  <p>&lt;------------------------------------------------------SOLUTION--------------------------------------------------&gt;</p>