将数据框附加到现有数据框中

append dataframe in to existing dataframe with condition

我现在的目标是我有两个数据集,我想清理它们并写入现有的 excel 文件而不覆盖。

因为我会多次重复这个过程,并且它有一个带日期的列,所以我想将数据集与最新的部分相匹配,并且只将最新的部分添加到 excel 文件中。因为这应该花费最少的时间。

例如,今天我可以从 2-02 提取数据到 2-19,但明天我也会从 2-02 提取数据到 2-20,但是我只想将 2-20 写入其中 excel.

但我只是不知道如何实现它。你们有什么想法可以实现吗?

您可以将现有的 Excel 文件导入数据框 df1。假设您更新的数据集称为 df2,您可以将新数据集过滤到大于 df1.

中最后一个日期的日期

因为我不知道你的数据集是什么样的,我假设有一列 'Date' 或类似的东西,它也在 pandas 中格式化为日期时间。

df1 = pd.read_excel(<your excel file>)

df2_new = df2[df2['Date'] > df1['Date'].max()] # filter df2
df_new = pd.concat([df1, df2_new]) # Concat new part with old part
df_new.to_excel(<your excel file>) # Export back to Excel-file

这可能是使用(仅)Pandas 的最简单方法。如果 dataset/Excel-file 变得非常大,更新现有的 Excel-file bij 可能会更有效,并使用 Excelwriter 或类似的包添加新行。