[英]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 实现的目标开始。
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.