Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Finding the maximum value between two columns where one of them is shifted and change the value of last row

$
0
0

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?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>