python - Pandas & Python:有没有办法导入excel文件的内容,在文件末尾添加文本内容,然后保存下来?
问题描述
我有一个存储在 excel 文件中的名称列表。用户需要能够导入该列表,这是一个单列,并向列表中添加其他名称,并保存文件。
我已经使用 pandas 导入了 excel 文件并创建了一个数据框(df)。我尝试使用循环函数附加 df 。
import numpy as np
import pandas as pd
path = 'C:\\NY_Operations\\EdV\\Streaman\\Python\\CES Fee Calc\\'
file_main = 'Main.xlsx'
df_main = pd.read_excel(path + file_main)
while True:
b = (input("Enter name of new deal to be added to 'Main' spreadsheet or type 'Exit' "))
df_main.append(b)
if df_main [-1] == "Exit":
df_main.pop()
break
电子表格在 A1、A2 和 A3 中有“玩具”、“颜色”、“球”。应提示用户添加新交易,他/她添加“Watch”和“Belt”,然后写入“Exit”,循环结束。在电子表格中,A4 应显示“Watch”,A5 应在 df 和电子表格中显示“Belt”。
解决方案
我能够弄清楚。感谢大家的帮助。
import numpy as np
import pandas as pd
#create list of deals in the 'main' consolidation spreadsheet
path = 'C:\\NY_Operations\\EdV\\Streaman\\Python\\CES Fee Calc\\'
file_main = 'Main.xlsx'
df_main = pd.read_excel(path + file_main)
new_deals = [] #each entry is the name of the item purchased
while True:
g = (input("Enter name of item or exit "))
new_deals.append(g)
if new_deals [-1] == "exit":
new_deals.pop()
break
df_newdeals = pd.DataFrame({'Deal Name':new_deals})
df1 = pd.concat([df_main,df_newdeals])
print(df1)
df1.to_excel(path + file_main)
推荐阅读
- reactjs - 使用 yalc 时如何显示代码更改
- substrate - 无效参数:无效长度 1,预期大小为 2 的元组
- python - 是否可以选择要在 Python 中打印的字典键?
- excel - MacOs 上的 VBA 函数 ActiveWorkbook.SaveAs
- angular - Angular Material Dialog,无法动态更新数据
- python - @staticmethod 是否在 CPython 或 Micropython 中保存任何 ram?
- matlab - Matlab 和 fortran 精度
- javascript - 为什么按钮可见性首先发生变化,而标签可见性第二,延迟很大?
- graph - Grafana图形跳跃/刷新时闪烁
- sql - 查询以使用进入条件填充排行榜