I need to write specific data in two excel sheets, the first sheet will be filled by the first and last date, while the second sheet will contain the time difference between two specific rows(only calculate the time difference when df.iloc[i, 1] == '[1]->[0]' and df.iloc[i + 1, 1] == '[0]->[1]'
).
This is my code:
import xlsxwriter
import pandas as pd
df= pd.DataFrame({'Time':['2019/01/03 15:02:07', '2019/01/03 15:16:55', '2019/01/03 15:17:20', '2019/01/03 15:28:58','2019/01/03 15:32:28','2019/01/03 15:38:54'],
'Payload':['[0]->[1]', '[1]->[0]','[0]->[1]','[0]->[1]','[1]->[0]','[0]->[1]']})
workbook = xlsxwriter.Workbook('Results.xlsx')
ws = workbook.add_worksheet("Rapport détaillé")
# wsNavco = workbook.add_worksheet("Délai reconnexion NAVCO")
ws.set_column(0, 1, 30)
ws.set_column(1, 2, 25)
# Add a format. Light yellow fill with dark red text.
format1 = workbook.add_format({'bg_color': '#fffcad',
'font_color': '#0a0a0a'})
# Add a format. Green fill with dark green text.
format2 = workbook.add_format({'bg_color': '#e7fabe',
'font_color': '#0a0a0a'})
# Write a conditional format over a range.
ws.conditional_format('A1:A24', {'type': 'cell',
'criteria': '>=',
'value': 50,
'format': format1})
ws.conditional_format('B1:B24', {'type': 'cell',
'criteria': '>=',
'value': 50,
'format': format2})
parametres = (
['Parametres', 'Valeurs'],
['1ere date ', str(df['Time'].iloc[0])],
['Derniere date ', str(df['Time'].iloc[len(df)-1])],
)
# Start from the first cell. Rows and
# columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for name, parametres in (parametres):
ws.write(row, col, name)
ws.write(row, col + 1, parametres)
row += 1
workbook.close()
df= df.sort_values(by='Time')
df.Time = pd.to_datetime(df.Time, format='%Y/%m/%d %H:%M:%S')
print('df\n',df)
diff = []
for i in range(len(df) - 1):
if df.iloc[i, 1] == '[1]->[0]' and df.iloc[i + 1, 1] == '[0]->[1]':
time_diff = df.iloc[i + 1, 0] - df.iloc[i, 0]
else:
time_diff = 0
diff.append(time_diff)
diff.append(0) # to fill the last value
df['Difference'] = diff
print(df['Difference'])
print('df1\n',df)
workbook = xlsxwriter.Workbook('Results.xlsx')
wsNavco = workbook.add_worksheet('Délai reconnexion NAVCO')
# wsNavco = wb.worksheets[1]
wsNavco.set_column(0, 1, 25)
wsNavco.set_column(1, 2, 55)
# Add a format. Light yellow fill with dark red text.
format1 = workbook.add_format({'bg_color': '#fffcad',
'font_color': '#0a0a0a'})
# Add a format. Green fill with dark green text.
format2 = workbook.add_format({'bg_color': '#e7fabe',
'font_color': '#0a0a0a'})
# Write a conditional format over a range.
wsNavco.conditional_format('A1:A24', {'type': 'cell',
'criteria': '>=',
'value': 50,
'format': format1})
wsNavco.conditional_format('B1:B24', {'type': 'cell',
'criteria': '>=',
'value': 50,
'format': format2})
for i in range (len(df)-1):
wsNavco.set_column(1, 1, 15)
wsNavco.write('A'+str(3),'Payload')
wsNavco.write('A'+str(i+4), str((df.iloc[i,1])))
wsNavco.write('B'+str(3),'Délai reconnexion NAVCO')
wsNavco.write('B'+str(i+4), str((df.iloc[i,2])))
workbook.close()
The problem is that it creates the first sheet and name and fill it, but then it overwrites the first sheet by the second sheet.
My question is: how can I save both sheets.