简体   繁体   English

Pandas:根据新创建的列中的上述行创建新列

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

I have a two-column numerical dataframe , and I'm trying to add a 3rd column.我有一个两列的数字数据框,我正在尝试添加第三列。

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

Where in the first row, col3 = max(col1 - col2,0) and on the rest of the rows, col3 = max(col1 - col2 + col3_of_the_row_above, 0)在第一行, col3 = max(col1 - col2,0)和其余的行, col3 = max(col1 - col2 + col3_of_the_row_above, 0)

The resulting dataframe should look like this:生成的数据框应如下所示:

    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

Is there an efficient way to do this?有没有一种有效的方法来做到这一点?

To create a new column you can just do this:要创建一个新列,您可以这样做:

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

col3 will be added in you dataframe. col3 将添加到您的数据框中。

Because the calculation method of your first row is different of the others, you'll need to this manually.由于您第一行的计算方法与其他行不同,因此您需要手动进行此操作。

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

The calculation method of the other rows is the same, so you can do this with a for iteration.其他行的计算方法相同,因此您可以使用 for 迭代来执行此操作。

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

PS: You can do this using list comprehension too, maybe it's too early, but I'll put the code too so you can study the code. 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))]

This is a more pythonic way to this, but it can be a little confusing at first sight.这是一种更加 Pythonic 的方式,但乍一看可能有点令人困惑。

Try this:尝试这个:

# 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

Output:输出:

     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.

相关问题 Pandas:如何使用现有列和新创建列中的先前行创建新列? - Pandas : How can I create new column using previous rows from existing column and newly created column? 根据 pandas 中的前一行创建新的平均列 - Create new average column based on previous row in pandas 大熊猫-根据“下一个”行值创建新列 - pandas - create new column based off of 'next' row value 如何基于熊猫中的行值创建新列 - How to create a new column based on row value in pandas 熊猫数据框应用功能基于选定的行创建新列 - Pandas dataframe apply function to create new column based on selected row 熊猫根据行值(条件)创建新列 - pandas create new column based on row value (condition) 根据上一行的值在熊猫数据框中创建一个新列 - Create a new column in a pandas dataframe based on values found on a previous row Pandas - 基于其他列创建新列,不包括第一行 - Pandas - create new column based on other columns, excluding first row 我想选择一列每一行的前 4 个单词,并根据该值使用 python 为另一个新创建的列分配一个新值 - I want to to pick the first 4 words of each row of a column and based on the value assign a new value to another newly created column using python Pandas dataframe 使用基于上述行的值创建新列 - Pandas dataframe create new columns with values based on above row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM