每次调用该函数时,都会在Excel文件中写入2行,但是在应用程序的末尾,文件中只有最后2行。每次我调用函数时,新的两行都会覆盖最后两行。我认为是因为这一行:
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)
来自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
来自功能
# 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)
输出:
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+
有人知道我该如何解决这个问题?我是否必须将df_write文件保存在main.py中,以便在for-slice结束之前不保存Excel文件?
经过长期搜索,我找到了一个可行的解决方案。
功能:
df1 = pd.DataFrame([['Wickelfalzrohr isoliert', 'R-R', df_read.at[row_counter, 'D'], counter_with_isolation ]],
columns=['Bez', 'KZ', 'D','L'])
df2 = pd.DataFrame([['Wickelfalzrohr nicht isoliert', 'R-R', df_read.at[row_counter, 'D'], counter_without_isolation ]],
columns=['Bez', 'KZ', 'D','L'])
df3 = pd.concat([df1, df2])
return df3
Main.py
# 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)
#Path to save the new Excel file
df_write_path = 'Data/Bauteilliste_sortiert.xlsx'
row_counter = 0
df4 = []
for row in df_read['KZ']:
if df_read.at[row_counter, 'KZ'] == 'R-R':
row_append = sort_component(df_read, df_read_path, df_write_path, row_counter, 'L')
df4.append(row_append)
row_counter += 1
else:
row_counter += 1
df_write = pd.concat(df4, ignore_index=True)
df_write.to_excel('Data/Bauteilliste_concat.xlsx')