Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. wb = Workbook()
  4. ws = wb.active
  5.  
  6. df_data = {
  7. 'year_month': ['2019-06', '2019-06', '2019-06', '2019-06', '2019-06',],
  8. 'lead_source': ['C', 'IH', 'INH', 'INH', 'MV'],
  9. 'status': ['Lead', 'Lead', 'Lead', 'Refund', 'Lead'],
  10. 'leads': [12, 7, 51, 2, 15],
  11. 'total': [140, 280, 918, 36, 150]
  12. }
  13. df = pd.DataFrame(df_data)
  14.  
  15. # Does work
  16. try:
  17. ws['A1'] = df[(df['lead_source'] == 'C') & (df['status'] == 'Refund')].iloc[0]['total']
  18. except IndexError:
  19. ws['A1'] = ''
  20.  
  21. # Doesn't work
  22. def cell_insert(cell, data):
  23. try:
  24. ws[cell] = data
  25. except IndexError:
  26. ws[cell] = ''
  27.  
  28. cell_insert('A2', df[(df['lead_source'] == 'C') & (df['status'] == 'Refund')].iloc[0]['total'])
  29.  
  30. wb.save(r"stackoverflow.xlsx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement