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?