Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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!

enter image description here

like image 886
kokodee Avatar asked Apr 05 '18 20:04

kokodee


Video Answer


2 Answers

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:

Expected Output after first section of code

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:

enter image description here

like image 178
patrickjlong1 Avatar answered Sep 17 '22 16:09

patrickjlong1


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)
like image 26
Elmspace Avatar answered Sep 16 '22 16:09

Elmspace