Each time the function is called, 2 rows are written in the Excel file, but at the end of the application there is only the last 2 row in the file. Every time i call the Function the two new row overwritte the last two rows. I think it's because of this line:
df_write = pd.concat([df_write, df1], ignore_index=True)
df_write = pd.concat([df_write, df2], ignore_index=True)
df_write.to_excel(df_write_path)
from main.py
import pandas as pd
# Open file
df_read_path = 'Data/Bauteilliste.xlsx'
df_read = pd.read_excel(df_read_path)
# Set display definitions on Jupyter
pd.set_option('display.max_columns', 75)
# Create file to write sorted row's from df_read
df_write = pd.DataFrame([],
[],
columns=['Bez', 'KZ', 'A', 'B', 'D','D1','D2','D3', 'L', 'Stk', 'IsoOf', 'LtgTyp'])
df_write_path = 'Data/Bauteilliste_sortiert.xlsx'
row_counter = 0
for row in df_read['KZ']:
if df_read.at[row_counter, 'KZ'] == 'R-R':
sort_component(df_read, df_write, df_read_path, df_write_path, row_counter, 'L')
row_counter += 1
else:
row_counter += 1
from function
# append new row to df_write with filtered R-R Element
df_write = df_write.append({
'Bez': 'Wickelfalzrohr nicht isoliert',
'KZ': 'R-R',
'D': df_read.at[row_counter, 'D'],
'L': counter_without_isolation
}, ignore_index=True, sort=False)
df_write = df_write.append({
'Bez': 'Wickelfalzrohr',
'KZ': 'R-R',
'D': df_read.at[row_counter, 'D'],
'L': counter_with_isolation
}, ignore_index=True, sort=False)
df_write = pd.concat([df_write, df1], ignore_index=True)
df_write = pd.concat([df_write, df2], ignore_index=True)
df_write.to_excel(df_write_path)
Output:
before row
160.0
before row
355.0
before row
200.0
before row
100.0
before row
125.0
before row
100.0
before row
200.0
before row
250.0
before row
160.0
before row
250.0
before row
125.0
before row
125.0
before row
160.0
before row
125.0
df_write excel file with 2 rows, it schould be 10+
Does anyone know how I can solve the problem? Do I have to save the df_write file in main.py so that the Excel file is not saved until the end of the for-slice?