簡體   English   中英

如何遍歷行並將基於上一行的值插入新列

[英]How to Iterate over rows and insert a value based on previous row to a new column

我有一些DataFrame,我正在嘗試使用上一行和當前行的計算值添加一個新列。
問題是我對每一行都得到相同的結果(當它需要不同時)

我做了一個將CSV轉換為df並進行更改的函數。

def index_csv_update(file_path):
    df = pd.read_csv(file_path)
    df.drop(["Adj Close"], axis=1, inplace=True)
    df.drop(["Volume"], axis=1, inplace=True)
    print(df.head())
    start = 0
    for i, row in df.iterrows():
        first_row = df.iloc[start, 4]
        try:
            second_row = df.iloc[start + 1, 4]
        except IndexError:
            second_row = 'null'
        if not second_row == 'null':
            df['Daily Change %'] = np.float((second_row/first_row)-1)
        start += 1
    df.to_csv(file_path, index=False)

打印結果:

         Date   Open   High    Low  Close  Daily Change %
0  2018-07-09  13.02  13.22  12.60  12.69        0.011575
1  2018-07-10  12.52  13.21  11.93  12.64        0.011575
2  2018-07-11  14.05  14.15  13.09  13.63        0.011575
3  2018-07-12  13.07  13.33  12.42  12.58        0.011575
4  2018-07-13  12.39  12.97  11.62  12.18        0.011575

在“ Daily Change %列上,應該有不同的數字。 我找不到問題,
請幫助謝謝。

您會在使用時看到

df['Daily Change %'] = np.float((second_row/first_row)-1)

您使用值..創建一個新列,您需要使用loc或iloc。

import pandas as pd
import numpy as np
df = pd.DataFrame({'foo': [1,2,3,3], 'bar': [1.2,2.3,3.5,1.3], 'foo2':[None, None, None, None]})
print(df)

start=0
for i, row in df.iterrows():
        first_row = df.iloc[start, 1]
        try:
            second_row = df.iloc[start + 1, 1]
        except IndexError:
            second_row = 'null'
        if not second_row == 'null':
            df.iloc[start,2] = np.float((second_row/first_row)-1)
        start += 1
print(df)

輸出:

   foo  bar  foo2
0    1  1.2  None
1    2  2.3  None
2    3  3.5  None
3    3  1.3  None


   foo  bar      foo2
0    1  1.2  0.916667
1    2  2.3  0.521739
2    3  3.5 -0.628571
3    3  1.3      None

暫無
暫無

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

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