简体   繁体   English

三列向量化操作

[英]Vectorized operation on three columns

First, let us create random dataframe:首先,让我们创建随机 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:然后,我使用minmax函数来创建两个额外的列:

df['max'] = df[['A', 'B', 'C']].max(axis=1)
df['min'] = df[['A', 'B', 'C']].min(axis=1)

Output: 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:什么是最有效和最优雅的方式来获得“中间”列的剩余价值,以便 output 看起来像这样:

    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 ?你应该使用median吗?

df[["A","B","C"]].median(axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM