我是靠谱客的博主 甜美衬衫,这篇文章主要介绍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

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

最后

以上就是甜美衬衫最近收集整理的关于python保存数据框_使用python pandas将新的数据框添加到现有的excel表中的全部内容,更多相关python保存数据框_使用python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部