My DataFrame is:
df = pd.DataFrame( {'a': [20, 9, 31, 40],'b': [1, 10, 17, 30], })Expected output: Creating column c and name
a b c name0 20 1 20 NaN1 9 10 20 NaN2 31 17 17 NaN3 40 30 40 a Steps:
a) c is created by df['c'] = np.fmax(df['a'].shift().bfill(), df['b'])
b) for the last row: df['c'] = df[['a', 'b']].max(). Since for the last row a > b 40 is chosen.
c) Get the name of max value between a or b for the last row.
My attempt:
df['c'] = np.fmax(df['a'].shift().bfill(), df['b'])df.loc[df.index[-1], 'c'] = df.loc[df.index[-1], ['a', 'b']].max()df.loc[df.index[-1], 'name'] = df.loc[df.index[-1], ['a', 'b']].idxmax()Is it the cleanest way / best approach?