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 nones created helper df replace , ffill (fillna method ffill - forward filling nans , nones):

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

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -