如何在不覆盖现有数据框的情况下附加数据框
How to append a dataframe without overwriting existing dataframe
每次调用该函数时,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_read.head()
df_write excel file with 2 rows, it schould be 10+
有谁知道我该如何解决这个问题?
我是否必须将 df_write 文件保存在 main.py 中,以便 Excel 文件直到 for-slice 结束时才保存?
经过长时间的搜索,我找到了一个有效的解决方案。
函数:
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')
每次调用该函数时,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_read.head()
df_write excel file with 2 rows, it schould be 10+
有谁知道我该如何解决这个问题? 我是否必须将 df_write 文件保存在 main.py 中,以便 Excel 文件直到 for-slice 结束时才保存?
经过长时间的搜索,我找到了一个有效的解决方案。
函数:
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')