简体   繁体   English

熊猫根据另一列中的值创建新列,如果为False,则返回新列的先前值

[英]Pandas Create New Column Based on Value in Another Column, If False Return Previous Value of New Column

this is a Python pandas problem I've been struggling with for a while now. 这是我一直在努力的Python熊猫问题。 Lets say I have a simple dataframe df where df['a'] = [1,2,3,1,4,6] and df['b'] = [10,20,30,40,50,60]. 可以说我有一个简单的数据帧df,其中df ['a'] = [1,2,3,1,4,6]和df ['b'] = [10,20,30,40,50,60] 。 I would like to create a third column 'c', where if the value of df['a'] == 1, df['c'] = df['b']. 我想创建第三列'c',如果df ['a'] == 1,则df ['c'] = df ['b']。 If this is false, df['c'] = the previous value of df['c']. 如果为假,则df ['c'] = df ['c']的先前值。 I have tried using np.where to make this happen, but the result is not what I was expecting. 我尝试使用np.where来实现此目的,但是结果却不是我所期望的。 Any advice? 有什么建议吗?

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))

The result is: 结果是:

   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

Whereas I would have expected: 而我原本希望:

   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

Try this: 尝试这个:

df.c.ffill(inplace=True)

Output: 输出:

   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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM