python - Conditionally replacing blank values in pandas dataframe -
i have dataframe has sparsely populated column. of values blank; other values 'buy' , 'sell.' replace blank values 'long' if last non-blank value 'buy' or 'short' if last non-blank value 'sell.' can enough in loop wondering if there non-loopy way of accomplishing this?
you can use fillna
or combine_first
replace none
s created helper df
replace
, ffill
(fillna
method ffill
- forward filling nan
s , none
s):
np.random.seed(12) df = pd.dataframe({'a':np.random.choice(['buy','sell', none], 10, p=(.2,.2,.6)), 'b':np.random.choice(['buy','sell', none], 10, p=(.2,.2,.6)), 'c':np.random.choice(['buy','sell', none], 10, p=(.2,.2,.6))}) print (df) b c 0 buy sell none 1 none none buy 2 sell none buy 3 none none buy 4 buy buy sell 5 none none none 6 none none none 7 buy none none 8 none none sell 9 buy buy none df = df.fillna(df.replace({'sell':'short', 'buy':'long'}).ffill()) #alternative solution #df = df.combine_first(df.replace({'sell':'short', 'buy':'long'}).ffill()) print (df) b c 0 buy sell none 1 long short buy 2 sell short buy 3 short short buy 4 buy buy sell 5 long long short 6 long long short 7 buy long short 8 long long sell 9 buy buy short
explanation:
print (df.replace({'sell':'short', 'buy':'long'})) b c 0 long short none 1 none none long 2 short none long 3 none none long 4 long long short 5 none none none 6 none none none 7 long none none 8 none none short 9 long long none print (df.replace({'sell':'short', 'buy':'long'}).ffill()) b c 0 long short none 1 long short long 2 short short long 3 short short long 4 long long short 5 long long short 6 long long short 7 long long short 8 long long short 9 long long short
Comments
Post a Comment