Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

how to calculate the rate of return for a list of prices in excel using python?

I am trying to get a rate of return for a list of prices in excel using python. I wrote the following code but I can't find out what the problem is.

for row in range(2, sheet.max_row + 1):
    close = sheet.cell(row, 5)
    returned = sheet.cell(row, 7)
    operation = pd.DataFrame.pct_change(close)
    returned.value = operation

enter image description here

this is the error that I keep getting: axis = self._get_axis_number(kwargs.pop("axis", self._stat_axis_name)) AttributeError: 'Cell' object has no attribute '_get_axis_number'

Any help would be appreciated.

question from:https://stackoverflow.com/questions/65713113/how-to-calculate-the-rate-of-return-for-a-list-of-prices-in-excel-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you are using pct_change for Close, you didn't need to going through loop. Just simple like that:

import pandas as pd

df = pd.read_excel('StackOverFlow.xlsx', sheet_name='Sheet1')

df['Operation'] = df['Close'].pct_change()

df

enter image description here

Besides that, I suggest you utilize pandas dataframe by field name, it could be easier for maintenance

for index, row in df.iterrows():
    close = row['Close']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...