簡體   English   中英

Pandas:根據新創建的列中的上述行創建新列

[英]Pandas: create new column based on above row in the newly created column

我有一個兩列的數字數據框,我正在嘗試添加第三列。

    Row    col1    col2
 
    0      8       8      
    1      8       4   
    2      6       2   
    3      3       7   
    4      6       4   
    5      2       6  

在第一行, col3 = max(col1 - col2,0)和其余的行, col3 = max(col1 - col2 + col3_of_the_row_above, 0)

生成的數據框應如下所示:

    Row    col1    col2    col3
 
    0      8       8       0   
    1      8       4       4
    2      6       2       8
    3      3       7       4
    4      6       4       6
    5      2       6       2

有沒有一種有效的方法來做到這一點?

要創建一個新列,您可以這樣做:

 df['col3'] = 0 # all the rows will be filled with zeros

col3 將添加到您的數據框中。

由於您第一行的計算方法與其他行不同,因此您需要手動進行此操作。

df['col3'][0] = max(df['col1'][0] - df['col2'][0], 0)

其他行的計算方法相同,因此您可以使用 for 迭代來執行此操作。

 for row in range(1, len(df)):
        df['col3'][row] = max(df['col1'][row] - df['col2'][row] + df['col3'][row - 1], 0)

PS:你也可以用list comprehension來做到這一點,也許現在還為時過早,但我也會把代碼放出來,這樣你就可以研究代碼了。

df['col3'] = 0 # all the rows will be filled with zeros
df['col3'] = [max(df['col1'][row] - df['col2'][row] + df['col3'][row - 1], 0) if row > 0 else max(df['col1'][row] - df['col2'][row], 0) for row in range(len(df))]

這是一種更加 Pythonic 的方式,但乍一看可能有點令人困惑。

嘗試這個:

# Calculate value for first row clip lower value to zero
s = (df.iloc[0, df.columns.get_loc('col1')] - df.iloc[0, df.columns.get_loc('col2')]).clip(0,)

# Calculate difference for each row after first
df['col3'] = (df.iloc[1:, df.columns.get_loc('col1')] - df.iloc[1:, df.columns.get_loc('col2')])

# Fill 'col3' with first value then cumsum differences
df['col3'] = df['col3'].fillna(s).cumsum()

df

輸出:

     col1  col2  col3
Row                  
0       8     8   0.0
1       8     4   4.0
2       6     2   8.0
3       3     7   4.0
4       6     4   6.0
5       2     6   2.0

暫無
暫無

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

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