简体   繁体   English

Python Pandas Dataframe 根据同一列中的前一行值计算新行值

[英]Python Pandas Dataframe calculating new row value based on previous row value within same column

It's a bit hard to explain, so I'll start off with what I'm trying to achieve using excel.这有点难以解释,所以我将从我试图使用 excel 实现的目标开始。

Excel示例

Basically, the value of the "Active" column is based on values of the same row different column values (columns 'Act Count' and 'De Count'), as well as the value in the previous row of the "Active" column.基本上,“Active”列的值基于同一行不同列值(“Act Count”和“De Count”列)的值,以及“Active”列的前一行中的值。

From the excel formula, if 'Act Count' < 4 and 'De Count' < 4, 'Active' = the previous row's 'Active' value.从 excel 公式中,如果 'Act Count' < 4 和 'De Count' < 4,则 'Active' = 前一行的 'Active' 值。

I want to transition this to Python pandas dataframe.我想将其转换为 Python pandas 数据框。

Here is the sample data:这是示例数据:

import pandas as pd

df = pd.DataFrame({'Act Count':[1,2,3,4,0,0,0,0,0,0,0,0,0,0],
              'De Count':[0,0,0,0,0,0,0,0,1,2,3,4,5,6]})

You can assume the first row value of 'Active' = 0.您可以假设第一行值 'Active' = 0。

I know of .shift() function, however I feel like I can't use it because I can't shift a column that doesn't exist yet.我知道 .shift() 函数,但是我觉得我无法使用它,因为我无法移动尚不存在的列。

This is not an elegant solution, but it will do the job.这不是一个优雅的解决方案,但它可以完成工作。

import pandas as pd

act_count = [1,2,3,4,0,0,0,0,0,0,0,0,0,0]
de_count = [0,0,0,0,0,0,0,0,1,2,3,4,5,6]

active = [0]
for i in range(1:len(act_count)):
    if act_count[i] >= 4:
       active.append(100)
    elif de_count[i] >= 4 :
       active.append(0)
    else:
       active.append(active[i-1])

df = pd.DataFrame({'Act Count': act_count, 'De Count' : de_count, 
              'Active' : active})

You can use this method:您可以使用此方法:

##Adding an empty column named Active to the existing dataframe
df['Active'] = np.nan

##putting the first value as 0
df['Active'].loc[0] = 0 

for index in range(1,df.shape[0]):
    if df['Act Count'].iloc[index]>=4:
        df['Active'].iloc[index]=100
    elif df['De Count'].iloc[index]>=4:
        df['Active'].iloc[index]=0
    else:
        df['Active'].iloc[index]=df['Active'].iloc[index-1]
print(df)

Output:输出:

   Act Count    De Count    Active
0   1           0           0.0
1   2           0           0.0
2   3           0           0.0
3   4           0           100.0
4   0           0           100.0
5   0           0           100.0
6   0           0           100.0
7   0           0           100.0
8   0           1           100.0
9   0           2           100.0
10  0           3           100.0
11  0           4           0.0
12  0           5           0.0
13  0           6           0.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas Dataframe基于前一行,将值添加到新列,但该列的最大值限于该列 - Pandas Dataframe Add a value to a new Column based on the previous row limited to the maximum value in that column 如何根据 Pandas dataframe 中上一行的行值创建新列? - How to create a new column based on row value in previous row in Pandas dataframe? 根据熊猫数据框中同一行的上一列值计算增加或减少的百分比 - Calculate the percentage increase or decrease based on the previous column value of the same row in pandas dataframe 根据前一行值对 Pandas Dataframe 进行排序 - Sort Pandas Dataframe based on previous row value Label 基于另一列(同一行)的值的列 pandas dataframe - Label a column based on the value of another column (same row) in pandas dataframe Pandas/Python:根据行值和其他 DataFrame 设置新列的值 - Pandas/Python: Set value of new column based on row value and other DataFrame 获取上一行的值并计算新列pandas python - get previous row's value and calculate new column pandas python 根据下一行的列值计算 dataframe 中的新列值 - Calculating new column value in dataframe based on next row's column value 根据列值删除Pandas中的DataFrame行 - Deleting DataFrame row in Pandas based on column value 迭代 dataframe 并根据一列的值在具有前一行值的新列中执行操作 - iterrate over dataframe and based on the value of one column do operations in a new column with previous row's value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM