我有一个非常简单的数据集,只有一列,我希望在数据帧的每一行上都有一个for循环,以便为每一行计算current_close_price/first_row_close_price
的日志。不管我做什么,上面写着:
TypeError:'numpy.foat64'对象不可调用
import pandas as pd
import numpy as np
price.head()
Close
Date
2010-07-19 107.290001
2010-07-20 108.480003
2010-07-21 107.070000
2010-07-22 109.459999
2010-07-23 110.410004
for index, row in price.iterrows():
first_row_price=price.iloc[0,0]
current_price=price.iloc[index,0]
log_rt = np.log(current_price / reference_price)
考虑我们在a.csv
文件中有一个表,它有两列Date和Close,并在代码中写入first_row_price
而不是reference_price
:
with open("a.csv", 'r') as a:
price = pd.read_csv(a, usecols=[1]) # which get data related to 'Close' column
for index, row in price.iterrows():
first_row_price = price.iloc[0, 0]
current_price = price.iloc[index, 0]
log_rt = np.log(current_price / first_row_price)
此代码将输出为:
0.0
0.011030393877764241
-0.002052631799009411
0.020023718610826604
0.02866528771045947