title: Pandas: for loop through columns
body: <p>My data looks like: </p>
  
  <pre><code>SNP Name ss715583617 ss715592335 ss715591044 ss715598181
  4    PI081762           T           A           A           T
  5   PI101404A           T           A           A           T
  6   PI101404B           T           A           A           T
  7    PI135624           T           A           A           T
  8    PI326581           T           A           A           T
  9   PI326582A           T           A           A           T
  10  PI326582B           T           A           A           T
  11   PI339732           T           A           A           T
  12  PI339735A           T           A           A           T
  13  PI339735B           T           A           A           T
  14  PI342618A           T           A           A           T
  </code></pre>
  
  <p>In reality I have a dataset of 50,000 columns of 479 rows. My objective is to go through each column with characters and convert the data to integers depending on which is the most abundant character.</p>
  
  <p>Right now I have the data input, and I have more or less written the function I would like to use to analyze each column separately. However, I can't quite understand how to use a forloop or use the apply function through all of the columns in the dataset. I would prefer not to hardcode the columns because I will have 40,000~50,000 columns to analyze. </p>
  
  <p>My code so far is:</p>
  
  <pre><code>import pandas as pd
  
  df = pd.read_csv("/home/dfreese/Desktop/testSNPtext", delimiter='\t')
  
  df.head() # check that the file format fits
  
  # ncol df
  df2 = df.iloc[4:-1] # Select the rows you want to analyze in a subset df
  print(df2)
  </code></pre>
  
  <p>My function: </p>
  
  <pre><code>def countAlleles(N): 
      # N is just suppose to be the column, ideally once I've optimized the function
      # I need to analyze every column 
  
      # Will hold the counts of each letter in the column
      letterCount = [] 
      # This is a parallel array to know the order
      letterOrder = {'T','A','G','C','H','U'}  
  
  
      # Boolean to use which one is the maximum 
      TFlag = None 
      AFlag = None 
      GFlag = None
      CFlag = None
      HFlag = None
      UFlag = None 
  
  # Loop through the column to determine which one is the maximum 
  for i in range(len(N)): # How do I get index information of the column?
      if(N[i] == 'T'): # If the element in the column is T
          letterCount[0] = letterCount[0] + 1
      elif(N[i] == 'A'):
          letterCount[1] = letterCount [1] + 1
      elif (N[i] == 'G'):
          letterCount[2] = letterCount [2] + 1
      elif (N[i] == 'C'):
          lettercount[3] = letterCount[3] + 1
      elif(N[i] == 'H'):
          letterCount[4] = letterCount[4] + 1
      else:
          letterCount[5] = letterCount[5] + 1
  
  max = letterCount[0] # This will hold the value of maximum
  mIndex = 0 # This holds the index position with the max value
  
  # Determine which one is max
  for i in range(len(letterCount)):
      if (letterCount[i] &gt; max):
          max = letterCount[i]
          mIndex = i
  </code></pre>
  
  <p>So I designed the function to input the column, in hopes to be able to iterate through all the columns of the dataframe. My main question is: </p>
  
  <p>1) How would I pass each in each column as a parameter to the for loop through the elements of each column?</p>
  
  <p>My major source of confusion is how indexes are being used in pandas. I'm familiar with 2-dimensional array in C++ and Java and that is most of where my knowledge stems from.</p>
  
  <p>I'm attempting to use the apply function: </p>
  
  <pre><code>df2 = df2.apply(countAlleles('ss715583617'), axis=2)
  </code></pre>
  
  <p>but it doesn't seem that my application is correct.</p>