简体   繁体   中英

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. 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]. I would like to create a third column 'c', where if the value of df['a'] == 1, df['c'] = df['b']. If this is false, df['c'] = the previous value of df['c']. I have tried using np.where to make this happen, but the result is not what I was expecting. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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