[英]Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas
[英]how to forward fill a column values based on the value in another column in same row
我想根据时间列向前填充金额列。 例如第一个值是 2800000.0 ,我希望这个值被填充 6 次。
amount times
2800000.0 6
nan 0 0
nan 0 0
nan 0 0
nan 0 0
nan 0 0
nan 0 0
4750000.0 4
nan 0 0
nan 0 0
nan 0 0
nan 0 0
nan 0 0
nan 0 0
期望的输出:
amount times
2800000.0 6
2800000.0 0
2800000.0 0
2800000.0 0
2800000.0 0
2800000.0 0
2800000.0 0
4750000.0 4
4750000.0 0
4750000.0 0
4750000.0 0
4750000.0 0
nan 0 0
nan 0 0
首先,创建通过测试非组缺少与累加和值,并传递给GroupBy.apply
与lambda函数与Series.ffill
与由第一值限制times
每基团:
#if necessary convert strings t onumeric and NaNs
#df['amount'] = pd.to_numeric(df['amount'], errors='coerce')
print (df['amount'].dtype)
float64
g = df['amount'].notna().cumsum()
f = lambda x: x['amount'].ffill(limit=x['times'].iat[0])
df['amount'] = df.groupby(g, group_keys=False).apply(f)
print (df)
amount times
0 2800000.0 6
1 2800000.0 0
2 2800000.0 0
3 2800000.0 0
4 2800000.0 0
5 2800000.0 0
6 2800000.0 0
7 4750000.0 4
8 4750000.0 0
9 4750000.0 0
10 4750000.0 0
11 4750000.0 0
12 NaN 0
13 NaN 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.