1

I'm looking for the equivalent of R's mutate, which allows you to reference defined variables immediately after creating them within the same mutate call.

new_df <- old_df %>%
    mutate(new_col = ifelse(something, 0, 1),
           newer_col = ifelse(new_col == 0, 'yay', 'nay'))

Looking for the equivalent in python pandas.

if I create the following dataframe, I was wondering if there is a way to use .assign to do the same thing?

dic = {'names': ['jeff', 'alice', 'steph', 'john'],
       'numbers':[4, 6, 5, 7]}

df = pd.DataFrame(dic)

df = df.assign(less_than_6 = np.where(df.numbers < 6, 100, 0),
               pass_fail = np.where(df.less_than_6 == 100, 'pass', 'fail'))

The alternative I can think of is..

df['less_than_6'] = np.where(df.numbers < 6, 100, 0)
df['pass_fail'] = np.where(df.less_than_6 == 100, 'pass', 'fail')

but was wondering if there is a way to do it in the same call?

Matt W.
  • 3,692
  • 2
  • 23
  • 46

1 Answers1

2

Using dict in assign

df.assign(**{'less_than_6' :lambda x : np.where(x['numbers'] < 6, 100, 0)}).assign(**{'pass_fail':lambda x : np.where(x['less_than_6'] == 100, 'pass', 'fail')})                                                            
Out[202]: 
   names  numbers  less_than_6 pass_fail
0   jeff        4          100      pass
1  alice        6            0      fail
2  steph        5          100      pass
3   john        7            0      fail
BENY
  • 317,841
  • 20
  • 164
  • 234
  • awesome, thank you. Can you explain what `**` is doing? – Matt W. Dec 18 '17 at 02:20
  • 1
    @MattW. ** is for kwargs, yo ucan check the link https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html also, https://stackoverflow.com/questions/2921847/what-does-the-star-operator-mean – BENY Dec 18 '17 at 02:23