title: Splitting a dataframe into separate CSV files
body: <p>I have a fairly large csv, looking like this:</p>
  
  <pre><code>+---------+---------+
  | Column1 | Column2 |
  +---------+---------+
  |       1 |   93644 |
  |       2 |   63246 |
  |       3 |   47790 |
  |       3 |   39644 |
  |       3 |   32585 |
  |       1 |   19593 |
  |       1 |   12707 |
  |       2 |   53480 |
  +---------+---------+
  </code></pre>
  
  <p>My intent is to </p>
  
  <ol>
  <li>Add a new column</li>
  <li>Insert a specific value into that column, 'NewColumnValue', on each row of the csv</li>
  <li>Sort the file based on the value in Column1</li>
  <li>Split the original CSV into new files based on the contents of 'Column1', removing the header</li>
  </ol>
  
  <p>For example, I want to end up with multiple files that look like:</p>
  
  <pre><code>+---+-------+----------------+
  | 1 | 19593 | NewColumnValue |
  | 1 | 93644 | NewColumnValue |
  | 1 | 12707 | NewColumnValue |
  +---+-------+----------------+
  
  +---+-------+-----------------+
  | 2 | 63246 | NewColumnValue |
  | 2 | 53480 | NewColumnValue |
  +---+-------+-----------------+
  
  +---+-------+-----------------+
  | 3 | 47790 | NewColumnValue |
  | 3 | 39644 | NewColumnValue |
  | 3 | 32585 | NewColumnValue |
  +---+-------+-----------------+
  </code></pre>
  
  <p>I have managed to do this using separate .py files:</p>
  
  <p>Step1</p>
  
  <pre><code># -*- coding: utf-8 -*-
  import pandas as pd
  df = pd.read_csv('source.csv')
  df = df.sort_values('Column1')
  df['NewColumn'] = 'NewColumnValue'
  df.to_csv('ready.csv', index=False, header=False)
  </code></pre>
  
  <p>Step2</p>
  
  <pre><code>import csv
  from itertools import groupby
  for key, rows in groupby(csv.reader(open("ready.csv")),
                           lambda row: row[0]):
      with open("%s.csv" % key, "w") as output:
          for row in rows:
              output.write(",".join(row) + "\n")
  </code></pre>
  
  <p>But I'd really like to learn how to accomplish everything in a single .py file.  I tried this: </p>
  
  <pre><code># -*- coding: utf-8 -*-
  #This processes a large CSV file.  
  #It will dd a new column, populate the new column with a uniform piece of data for each row, sort the CSV, and remove headers
  #Then it will split the single large CSV into multiple CSVs based on the value in column 0 
  import pandas as pd
  import csv
  from itertools import groupby
  df = pd.read_csv('source.csv')
  df = df.sort_values('Column1')
  df['NewColumn'] = 'NewColumnValue'
  for key, rows in groupby(csv.reader((df)),
                           lambda row: row[0]):
      with open("%s.csv" % key, "w") as output:
          for row in rows:
              output.write(",".join(row) + "\n")
  </code></pre>
  
  <p>but instead of working as intended, it's giving me multiple CSVs named after each column header.</p>
  
  <p>Is that happening because I removed the header row when I used separate .py files and I'm not doing it here?  I'm not really certain what operation I need to do when splitting the files to remove the header.</p>