Python3 - Loop over rows, then some columns to print text
<p>I have a pandas dataframe that looks something like this:</p>

<pre><code>            0                   1                   2              3  \
0  UICEX_0001   path/to/bam_T.bam   path/to/bam_N.bam     chr1:10000   
1  UICEX_0002  path/to/bam_T2.bam  path/to/bam_N2.bam  chr54:4958392   

              4  
0  chr4:4958392  
1           NaN 
</code></pre>

<p>I am trying to loop through each row and print text to output for another program.  I need to print the first three columns (with some other text), then go through the rest of the columns and print something different depending on if they are NaN or not.</p>

<p>This works mostly:</p>

<p><strong>Current code</strong></p>

<pre><code>def CreateIGVBatchScript(x):
    for row in x.iterrows():
        print("\nnew")
        sample = x[0]
        bamt = x[1]
        bamn = x[2]
        print("\nload", bamt.to_string(index=False), "\nload", bamn.to_string(index=False))
        for col in range(3, len(x.columns)):
            position = x[col]
            if position.isnull().values.any():
                print("\n")
            else:
                position = position.to_string(index=False)
                print("\ngoto ", position, "\ncollapse\nsnapshot ", sample.to_string(index=False), "_", position,".png\n")

CreateIGVBatchScript(data)
</code></pre>

<p>but the output looks like this:</p>

<p><strong>Actual Output</strong></p>

<pre><code>new
load path/to/bam_T.bam
path/to/bam_T2.bam 
load path/to/bam_N.bam
path/to/bam_N2.bam

goto  chr1:10000
chr54:4958392 
collapse
snapshot  UICEX_0001 **&lt;-- ISSUE: it's printing both rows at the same time**
UICEX_0002 _ chr1:10000
chr54:4958392 .png

new

load path/to/bam_T.bam
path/to/bam_T2.bam 
load path/to/bam_N.bam
path/to/bam_N2.bam

goto  chr1:10000
chr54:4958392 
collapse
snapshot  UICEX_0001   **&lt;-- ISSUE: it's printing both rows at the same time**
UICEX_0002 _ chr1:10000
chr54:4958392 .png
</code></pre>

<p>The first part seems fine, but when I start to iterate over the columns, all rows are printed.  I can't seem to figure out how to fix this.  Here's what I want one of those parts to look like:</p>

<p><strong>Partial Wanted Output</strong></p>

<pre><code>goto chr1:10000
collapse
snapshot UICEX_0001_chr1:10000.png
goto chr54:4958392
collapse
snapshot UICEX_0001_chr54:495832.png
</code></pre>

<p><strong>Extra information</strong>
Incidentally, I'm actually trying to adapt this from an R script in order to better learn Python.  Here's the R code, in case that helps:</p>

<pre><code>CreateIGVBatchScript &lt;- function(x){
     for(i in 1:nrow(x)){
          cat("\nnew")
          sample = as.character(x[i, 1])
          bamt = as.character(x[i, 2])
          bamn = as.character(x[i, 3])
          cat("\nload",bamt,"\nload",bamn)
          for(j in 4:ncol(x)){
               if(x[i, j] == "" | is.na(x[i, j])){ cat("\n") }
               else{
                    cat("\ngoto ", as.character(x[i, j]),"\ncollapse\nsnapshot ", sample, "_", x[i,j],".png\n", sep = "")
               }
          }
     }
     cat("\nexit")
}
CreateIGVBatchScript(data)
</code></pre>

<p>I've come up with the answer.  There are a few problems here:</p>

<ol>
<li>I was using <code>iterrows()</code> incorrectly.</li>
</ol>

<p>The iterrows object actually holds the information from the rows, and then you can use the index to save values from that Series.</p>

<pre><code>for index, row in x.iterrows():
    sample = row[0]
</code></pre>

<p>will save the value in that row in column 0.</p>

<ol start="2">
<li>Iterate over the columns</li>
</ol>

<p>At this point, you can use a simple for loop, as I was doing to iterate over the columns.</p>

<pre><code>for col in range(3, len(data.columns)):
    position = row[col]
</code></pre>

<p>lets you save a value from that column.</p>

<p>The final Python code is:</p>

<pre><code>def CreateIGVBatchScript(x):
    x=x.fillna(value=999)
    for index, row in x.iterrows():
        print("\nnew", sep="")
        sample = row[0]
        bamt = row[1]
        bamn = row[2]
        print("\nload ", bamt, "\nload ", bamn, sep="")
        for col in range(3, len(data.columns)):
            position = row[col]
            if position == 999:
                print("\n")
            else:
                print("\ngoto ", position, "\ncollapse\nsnapshot ", sample, "_", position, ".png\n", sep="")

CreateIGVBatchScript(data)
</code></pre>

<p>Answers were guided by the following posts:</p>

<ul>
<li><a href="http://stackoverflow.com/questions/28218698/how-to-iterate-over-columns-of-pandas-dataframe-to-run-regression">How to iterate over columns of pandas dataframe to run regression</a></li>
<li><a href="http://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe">How to iterate over rows in a DataFrame?</a></li>
<li><a href="http://stackoverflow.com/questions/29700552/series-objects-are-mutable-and-cannot-be-hashed-error">âSeries objects are mutable and cannot be hashedâ error</a></li>
<li><a href="http://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-newlines-or-spaces">How do I keep Python print from adding newlines or spaces?</a></li>
</ul>

python pandas