title: Writing single CSV header with pandas
body: <p>I'm parsing data into lists and using pandas to frame and write to an CSV file. First my data is taken into a set where <em>inv</em>, <em>name</em>, and <em>date</em> are all lists with numerous entries. Then I use <em>concat</em> to concatenate each iteration through the datasets I parse through to a CSV file like so:</p>
  
  <pre><code>counter = True
  data = {'Invention': inv, 'Inventor': name, 'Date': date}
  
  if counter is True:
    df = pd.DataFrame(data)
    df = df[['Invetion', 'Inventor', 'Date']]
  
  else:
    df = pd.concat([df, pd.DataFrame(data)])
    df = df[['Invention', 'Inventor', 'Date']]
  
    with open('./new.csv', 'a', encoding = utf-8) as f:
      if counter is True:
        df.to_csv(f, index = False, header = True)
      else:
        df.to_csv(f, index = False, header = False)
  
  counter = False
  </code></pre>
  
  <p>The counter = True statement resides <em>outside</em> of my iteration loop for all the data I'm parsing so it's not overwriting every time.</p>
  
  <p>So this means it only runs <em>once</em> through my data to grab the first <em>df</em> set then concats it thereafter. The problem is that even though counter is only True the first round and works for my first <em>if-statement</em> for df it does not work for my writing to file.</p>
  
  <p>What happens is that the header is written over and over again - regardless to the fact that counter is only True once. When I swap the header = False for when counter is True then it never writes the header.</p>
  
  <p>I think this is because of the concatenation of df holding onto the header somehow but other than that I cannot figure out the logic error.</p>
  
  <p>Is there perhaps another way I could also write a header once and only once to the same CSV file?</p>