transitioning from r to Python - dplyr-like operations in pandas -
i'm used using r. if had in r this:
library(dplyr) df = df %>% mutate( xyz = sum(x+y+z), weekcheck = ifelse( week > 3 & x*2 > 4, 'yes',week), # multi-step if statement xyz_plus_3 = xyz + 3 ) df = pd.dataframe({ 'x': np.random.uniform(1., 168., 20), 'y': np.random.uniform(7., 334., 20), 'z': np.random.uniform(1.7, 20.7, 20), 'month': [5,6,7,8]*5, 'week': np.random.randint(1,4, 20) })
i know theres assign can't figure out syntax chaining these operations together, particularly using ifelse sort of thing.
anyone attempt break down me? if don't know r think code common sense..
you'd need 2 assign calls , syntax not pretty:
(df.assign(xyz=df[['x', 'y', 'z']].sum(axis=1), weekcheck=np.where((df['week']>3) & (df['x']*2>4), 'yes', df['week'])) .assign(xyz_plus_3=lambda d: d['xyz']+3))
Comments
Post a Comment