First, let us create random dataframe:
df = pd.DataFrame(
{
"A": np.random.randint(0, 70, size=5),
"B": np.random.randint(-10, 35, size=5),
"C": np.random.randint(10, 50, size=5)
}
)
Then, I am using min and max functions to create two additional columns:
df['max'] = df[['A', 'B', 'C']].max(axis=1)
df['min'] = df[['A', 'B', 'C']].min(axis=1)
Output:
A B C max min
0 17 26 31 31 17
1 45 31 17 45 17
2 36 24 31 36 24
3 16 17 24 24 16
4 16 12 23 23 12
What would be the most efficient and elegant way to get remaining value to the 'mid' column so that the output looked like this:
A B C max min mid
0 17 26 31 31 17 26
1 45 31 17 45 17 31
2 36 24 31 36 24 31
3 16 17 24 24 16 17
4 16 12 23 23 12 16
I am looking for vectorized solution. I was able to achieve this using conditions:
conditions = [((df['A'] > df['B']) & (df['A'] < df['C']) | (df['A'] > df['C']) & (df['A'] < df['B'])),
((df['B'] > df['A']) & (df['B'] < df['C']) | (df['B'] > df['C']) & (df['B'] < df['A'])),
((df['C'] > df['A']) & (df['C'] < df['B']) | (df['C'] > df['B']) & (df['C'] < df['A']))]
choices = [df['A'], df['B'], df['C']]
df['mid'] = np.select(conditions, choices, default=0)
However, I think there is more elegant solution for that.
Should you use median
?
df[["A","B","C"]].median(axis=1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.