簡體   English   中英

Pandas:計算兩行之間的百分比並將值添加為列

[英]Pandas: Calculate the percentage between two rows and add the value as a column

我有一個這樣結構的數據集:

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

此時間序列代表一般股票市場的價值。

我想計算“收盤”列的兩行之間的百分比差異(實際上,我想知道股票的價值增加或減少了多少;每行代表一天)。

我已經用 for 循環完成了這個(在大數據問題中使用 Pandas 很糟糕),我創建了正確的結果,但在不同的 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)

我怎樣才能利用 dataFrame api 重構它,從而刪除 for 循環並在適當的位置創建一個新列?

我建議首先將 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:

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

或者如果你想以相反的順序改變計算:

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

使用diff

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM