我将使用以下方法来处理这个问题。
1)使用如下函数导入Excel文件:
pandas.read_excel
从而将Excel中的数据转换为数据帧。我会打电话给你
exceldf
2)将此数据框与熊猫数据框中已有的数据合并。我将调用您现有的翻译数据帧
translateddf
3)重新排序新合并的数据帧
newdf
然后导出数据。有关如何重新排序的更多选项如下所示:
re-ordering data frame
4)将数据导出到Excel。我将让您把它集成到初始代码中。对于这个问题的一般答案,其他人可能希望在这里研究综合熊猫选项。
to_excel
示例代码
import pandas
# Read in the Excel file
exceldf = pandas.read_excel(open('your_xls_xlsx_filename'), sheetname='Sheet 1')
# Create a new dataframe with your merged data, merging on 'key1'.
# We then drop the column of the original translation, as it should no longer be needed
# I've included the rename argument in case you need it.
newdf = exceldf.merge(translateddf, left_on=['key1'], \
right_on=['key1']) \
.rename(columns={'Original Translation {language}': 'Original Translation {language}'}) \
.drop(['Original Translation'], axis=1)
# Re-order your data.
# Note that if you renamed anything above, you have to update it here too
newdf = newdf[['0', '1', '2', 'Original Translation {language}']]
# An example export, that uses the generic implementation, not your specific code
pandas.newdf.to_excel("output.xlsx")