Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- wb = Workbook()
- ws = wb.active
- df_data = {
- 'year_month': ['2019-06', '2019-06', '2019-06', '2019-06', '2019-06',],
- 'lead_source': ['C', 'IH', 'INH', 'INH', 'MV'],
- 'status': ['Lead', 'Lead', 'Lead', 'Refund', 'Lead'],
- 'leads': [12, 7, 51, 2, 15],
- 'total': [140, 280, 918, 36, 150]
- }
- df = pd.DataFrame(df_data)
- # Does work
- try:
- ws['A1'] = df[(df['lead_source'] == 'C') & (df['status'] == 'Refund')].iloc[0]['total']
- except IndexError:
- ws['A1'] = ''
- # Doesn't work
- def cell_insert(cell, data):
- try:
- ws[cell] = data
- except IndexError:
- ws[cell] = ''
- cell_insert('A2', df[(df['lead_source'] == 'C') & (df['status'] == 'Refund')].iloc[0]['total'])
- wb.save(r"stackoverflow.xlsx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement