Pandas:计算两行之间的百分比,并将值作为列相加



我有一个结构如下的数据集:

"Date","Time","Open","High","Low","Close","Volume"

这个时间序列代表了一个普通股票市场的价值。

我想计算"收盘"列两行之间的百分比差异(事实上,我想知道股票的价值增加或减少了多少;每行代表一天(。

我用for循环完成了这项工作(在大数据问题中使用panda非常糟糕(,我创建了正确的结果,但使用了不同的DataFrame:

rows_number = df_stock.shape[0]
# The first row will be 1, because is calculated in percentage. If haven't any yesterday the value must be 1
percentage_df = percentage_df.append({'Date': df_stock.iloc[0]['Date'], 'Percentage': 1}, ignore_index=True)
# Foreach days, calculate the market trend in percentage
for index in range(1, rows_number):
# n_yesterday : 100 = (n_today - n_yesterday) : x
n_today = df_stock.iloc[index]['Close']
n_yesterday = self.df_stock.iloc[index-1]['Close']
difference = n_today - n_yesterday
percentage = (100 * difference ) / n_yesterday
percentage_df = percentage_df .append({'Date': df_stock.iloc[index]['Date'], 'Percentage': percentage}, ignore_index=True)

我如何利用dataFrameapi重构它,从而删除for循环并在适当的位置创建一个新列?

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

或者如果你想以相反的顺序计算变化:

df['Change'] = df['Close'].pct_change(-1)

我建议首先将Date列作为DateTime索引。为此,您可以使用

df_stock = df_stock.set_index(['Date'])
df_stock.index = pd.to_datetime(df_stock.index, dayfirst=True)

然后,只需使用日期时间索引访问具有特定列的任何行,并执行任何类型的操作,例如计算列"关闭"的两行之间的百分比差异

df_stock['percentage'] = ((df_stock['15-07-2019']['Close'] - df_stock['14-07-2019']['Close'])/df_stock['14-07-2019']['Close']) * 100

您也可以使用for循环为每个日期或行执行操作:

for Dt in df_stock.index:

使用diff

(-df['Close'].diff())/df['Close'].shift()

最新更新