here is my code:
import openpyxl, pprint
wb = openpyxl.load_workbook('/Users/sarahporgess/Desktop/SSA1.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
data = {}
for row in range(1,sheet.max_row+1):
date = sheet['A' +str(row)].value
gamma = sheet['B' +str(row)].value
theta = sheet['C' +str(row)].value
ratio = float(gamma)/float(theta)
resultFile = open('SSA2.csv' , 'w')
resultFile.write( pprint.pformat(date))
resultFile.write( pprint.pformat(gamma))
resultFile.write( pprint.pformat(theta))
resultFile.write( pprint.pformat(ratio))
print(ratio)
sheet['D1']=ratio
resultFile.close()
print('Done.')
my existing excel file currently has three columns: "date, gamma, theta". I want to add a fourth column called "ratio" that is the ratio of gamma/theta. How do I add another column to an existing excel document using python? This code creates an excel document with the 4 elements printed into one cell
It is easier to use the Pandas package
import pandas as pd
file_name = #Path to your file
df = pd.read_excel(file_name) #Read Excel file as a DataFrame
df['Ratio'] = df['Gamma']/df['Theta']
#Display top 5 rows to check if everything looks good
df.head(5)
#To save it back as Excel
df.to_excel("path to save") #Write DateFrame back as Excel file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With