python保存数据框_使用python pandas将新的数据框添加到现有的excel表中

它通过循环Excel文件的文件夹中, 消除了前两排, 然后保存出来为单独的Excel文件, 并且还节省了在循环作为一个附加文件的文件。

当前附加文件每次运行代码时都会覆盖现有文件。

我需要新的数据追加到的底部已经存在的Excel工作表('master_data.xlsx)

dfList = []

path = 'C:\\Test\\TestRawFile'

newpath = 'C:\\Path\\To\\New\\Folder'

for fn in os.listdir(path):

# Absolute file path

file = os.path.join(path, fn)

if os.path.isfile(file):

# Import the excel file and call it xlsx_file

xlsx_file = pd.ExcelFile(file)

# View the excel files sheet names

xlsx_file.sheet_names

# Load the xlsx files Data sheet as a dataframe

df = xlsx_file.parse('Sheet1',header= None)

df_NoHeader = df[2:]

data = df_NoHeader

# Save individual dataframe

data.to_excel(os.path.join(newpath, fn))

dfList.append(data)

appended_data = pd.concat(dfList)

appended_data.to_excel(os.path.join(newpath, 'master_data.xlsx'))

我以为这是一个简单的任务,但我想不会。 我想我需要将master_data.xlsx文件作为数据框引入,然后将索引与新添加的数据进行匹配,然后将其保存。或者也许有一个更简单的方法。任何帮助表示赞赏。

2016-06-28

brandog

+0

是[that](http://stackoverflow.com/a/36450435/5741205)你在做什么? –

+0

不,不是,我不是试图保存新的工作表,只是想追加现有的工作表。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值