这是我已经苦苦挣扎了一段时间的Python熊猫问题。可以说我有一个简单的数据帧df,其中df ['a'] = [1,2,3,1,4,6]和df ['b'] = [10,20,30,40,50,60] 。我想创建第三列'c',如果df ['a'] == 1,则df ['c'] = df ['b']。如果为假,则df ['c'] = df ['c']的先前值。我尝试使用np.where来实现此目的,但是结果却不是我所期望的。有什么建议吗?
df = pd.DataFrame()
df['a'] = [1,2,3,1,4,6]
df['b'] = [10,20,30,40,50,60]
df['c'] = np.nan
df['c'] = np.where(df['a'] == 1, df['b'], df['c'].shift(1))
结果是:
a b c
0 1 10 10.0
1 2 20 NaN
2 3 30 NaN
3 1 40 40.0
4 4 50 NaN
5 6 60 NaN
我原本期望:
a b c
0 1 10 10.0
1 2 20 10.0
2 3 30 10.0
3 1 40 40.0
4 4 50 40.0
5 6 60 40.0
答案 0 :(得分:2)
尝试一下:
df.c.ffill(inplace=True)
输出:
a b c
0 1 10 10.0
1 2 20 10.0
2 3 30 10.0
3 1 40 40.0
4 4 50 40.0
5 6 60 40.0