简体   繁体   中英

Python Pandas - How to write in a specific column in an Excel Sheet

I am having trouble updating an Excel Sheet using pandas by writing new values in it. I already have an existing frame df1 that reads the values from MySheet1.xlsx. so this needs to either be a new dataframe or somehow to copy and overwrite the existing one.

The spreadsheet is in this format:

在此处输入图像描述

I have a python list: values_list = [12.34, 17.56, 12.45]. My goal is to insert the list values under Col_C header vertically. It is currently overwriting the entire dataframe horizontally, without preserving the current values.

df2 = pd.DataFrame({'Col_C': values_list})
writer = pd.ExcelWriter('excelfile.xlsx', engine='xlsxwriter')
df2.to_excel(writer, sheet_name='MySheet1')
workbook  = writer.book
worksheet = writer.sheets['MySheet1']

How to get this end result? Thank you!

在此处输入图像描述

Below I've provided a fully reproducible example of how you can go about modifying an existing .xlsx workbook using pandas and the openpyxl module (link to Openpyxl Docs) .

First, for demonstration purposes, I create a workbook called test.xlsx:

from openpyxl import load_workbook
import pandas as pd
writer = pd.ExcelWriter('test.xlsx', engine='openpyxl') 
wb  = writer.book
df = pd.DataFrame({'Col_A': [1,2,3,4],
                  'Col_B': [5,6,7,8],
                  'Col_C': [0,0,0,0],
                  'Col_D': [13,14,15,16]})

df.to_excel(writer, index=False)
wb.save('test.xlsx')

This is the Expected output at this point:

第一段代码后的预期输出

In this second part, we load the existing workbook ('test.xlsx') and modify the third column with different data.

from openpyxl import load_workbook
import pandas as pd
df_new = pd.DataFrame({'Col_C': [9, 10, 11, 12]})
wb = load_workbook('test.xlsx')

ws = wb['Sheet1']

for index, row in df_new.iterrows():
    cell = 'C%d'  % (index + 2)
    ws[cell] = row[0]

wb.save('test.xlsx')

This is the Expected output at the end:

在此处输入图像描述

In my opinion, the easiest solution is to read the excel as a panda's dataframe, and modify it and write out as an excel. So for example:

Comments:

Import pandas as pd. Read the excel sheet into pandas data-frame called. Take your data, which could be in a list format, and assign it to the column you want. (just make sure the lengths are the same). Save your data-frame as an excel, either override the old excel or create a new one.

Code:

import pandas as pd
ExcelDataInPandasDataFrame = pd.read_excel("./YourExcel.xlsx")
YourDataInAList = [12.34,17.56,12.45]
ExcelDataInPandasDataFrame ["Col_C"] = YourDataInAList
ExcelDataInPandasDataFrame .to_excel("./YourNewExcel.xlsx",index=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM