title: Python: Output Data to Excel Spreadsheet
body: <p>I need to do what is probably very simple for an experienced coder. My Python program successfully does the following: </p>
  
  <ol>
  <li>Import an excel spreadsheet</li>
  <li>Iterate through the rows and count repeating occurrences of a "Model", whether the model (Location) is consumed or not, and who "consumed" the model (and how many they've consumed). Anything not "Consumed" will be considered "Available". </li>
  <li>Print the "Model", "Consumed", "Available", and "Requestor"(someone who's consumed the model). </li>
  </ol>
  
  <p>Instead of just printing the values, I need them to be added to a sheet on a excel workbook with Model, Consumed, Avaible, and Requestors as the column headers. </p>
  
  <p>Here's how I'm printing the values: </p>
  
  <pre><code>if not REQUESTORLIST:
      print(CURRENTMODEL, "Consumed:", CONSUMEDCOUNTER, "Available:", AVAILABLECOUNTER)
  else:
      print(CURRENTMODEL,"Consumed:",CONSUMEDCOUNTER, "Available:",AVAILABLECOUNTER,REQUESTORS)
  </code></pre>
  
  <p>Here are the data types:</p>
  
  <pre><code>print(type(CURRENTMODEL))
  print(type(CONSUMEDCOUNTER))
  print(type(AVAILABLECOUNTER))
  print(type(REQUESTORS))
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>&lt;class 'str'&gt;
  &lt;class 'int'&gt;
  &lt;class 'int'&gt;
  &lt;class 'collections.Counter'&gt;
  </code></pre>
  
  <p>Finally, program outputs truncated:</p>
  
  <pre><code>Model    WS-SFP Consumed: 1 Available: 2 Counter({'Requester    Anthony House': 1})
  </code></pre>
  
  <p>I'm brand new to programming (this is the first program I've written) and am having trouble finding a way to get these values to write to an excel sheet with the four column headers I need. I've tried to convert them to strings and use .write but have been unsuccessful so far. Do you have any suggestions? </p>
  
  <p>EDIT: Thanks for the quick responses. I'm thinking it may be helpful for me to just post my code. I'm open to feedback on how to optimize this thing as it's likely subpar. I've been experimenting with .write, which skips rows in the excel sheet output, doesn't fill column headers, etc. Probably not my best option. </p>
  
  <pre><code>import os
  import openpyxl
  import matplotlib
  import numpy
  import pandas as pd
  import xlrd
  import xlwt
  from xlwt import Workbook
  import xlsxwriter
  from collections import Counter
  
  #file to pull data from
  excel_file = 'Customer_Inventory_Test.xlsx'
  models = pd.read_excel(excel_file)
  #file to export results
  workbook = xlsxwriter.Workbook('Inventory Report.xlsx')
  worksheet = workbook.add_worksheet()
  
  row = 0
  col = 0
  ROWCOUNT = models.shape[0]
  
  while True:
      CONSUMEDCOUNTER = 0
      AVAILABLECOUNTER = 0
      REQUESTORLIST = []
      #break when no more rows
      if row == ROWCOUNT:
          break
      MODEL = models.iloc[row, [0]]
      #convert to string for comparison
      MODEL = MODEL.to_string()
      CURRENTMODEL = MODEL
      LOCATION = models.iloc[row, [2]]
      LOCATION = LOCATION.to_string()
  
      while CURRENTMODEL == MODEL:
          if "Consumed" in LOCATION:
              CONSUMEDCOUNTER += 1
              REQUESTOR = models.iloc[row, [17]]
              # convert to string for comparison
              REQUESTOR = REQUESTOR.to_string()
              REQUESTORLIST.append(REQUESTOR)
          else:
              AVAILABLECOUNTER += 1
          row += 1
          if row == ROWCOUNT:
              break
          MODEL = models.iloc[row, [0]]
          MODEL = MODEL.to_string()
          LOCATION = models.iloc[row, [2]]
          LOCATION = LOCATION.to_string()
  
      REQUESTORS = Counter(REQUESTORLIST)
  
      if not REQUESTORLIST:
          worksheet.write(row, col, CURRENTMODEL)
          worksheet.write(row, col + 1, CONSUMEDCOUNTER)
          worksheet.write(row, col + 2, AVAILABLECOUNTER)
          print(CURRENTMODEL[9:], "Consumed:", CONSUMEDCOUNTER, "Available:", 
  AVAILABLECOUNTER)
      else:
          worksheet.write(row, col, CURRENTMODEL)
          worksheet.write(row, col + 1, CONSUMEDCOUNTER)
          worksheet.write(row, col + 2, AVAILABLECOUNTER)
          #worksheet.write(row, col + 3, REQUESTORS) &lt;- Doesn't like 
  #requestors data structure
          print(CURRENTMODEL[9:],"Consumed:",CONSUMEDCOUNTER, 
  "Available:",AVAILABLECOUNTER,REQUESTORS)
  
  workbook.close()
  </code></pre>