简体   繁体   中英

Using Pandas, how can I find the min/max value and index from one set of columns, satisfying condition on a corresponding set of columns?

I have a DataFrame with two sets of columns that have matching names (x1, x2, ... and y1, y2, ...).

For each row in my DataFrame, I need to make a new column containing the min/max x column, such that y is minimised/maximised respectively.

Using Excel, I can get close the the desired result with this sort of formula:

=MINIFS(<x-columns>,<y-columns>,MIN(<y-columns>))

=MAXIFS(<x-columns>,<y-columns>,MAX(<y-columns>))

Although I would also need to make use of Pandas' idxmin and idxmax to get the column names.

As an example, the following row of data would need to return 55/x2 (min xi such that yi = ymin) and 56/x3 (max xi such that yi = ymax)

df = pd.DataFrame([[30, 55, 56, 73, 50, 3, 0, 3, 0, 3]], columns=['x1', 'x2', 'x3', 'x4', 'x5', 'y1', 'y2', 'y3', 'y4', 'y5'])

df['ymin'] = df.filter(regex='^y').min(axis=1)
df['ymax'] = df.filter(regex='^y').max(axis=1)

This is my approach, after several trials and errors:

new_df = (pd.wide_to_long(df.reset_index(), 
                stubnames=['x','y'], 
                i='index',
                j='xy')
            .reset_index()
            .drop('xy', axis=1)
            .groupby(['index', 'y'])['x'].agg(['max', 'min'])
            .groupby('index')
            .apply(lambda x: pd.Series(x.values[[0,-1], [1,0]],
                                       index=['ymin', 'ymax']) )
         )

Output:

       ymin  ymax
index            
0        55    56

Update : if you also want the column name, this can be an option:

new_df = (pd.wide_to_long(df.reset_index(), 
                stubnames=['x','y'], 
                i='index',
                j='xy')
            .reset_index()
         )

u = (new_df.groupby(['index', 'y'])['x'].agg(['idxmax','idxmin'])
         .groupby('index')
         .apply(lambda x: pd.Series(x.values[[0,-1], [1,0]],
                                       index=['ymin', 'ymax']) )    
    )

Then:

new_df.loc[u['ymin']]

gives:

   index  xy   x  y
1      0   2  55  0

and

new_df.loc[u['ymax']]

gives:

   index  xy   x  y
2      0   3  56  3

Thanks to Quang Hoang, I've managed to put together this function, which gives the result I wanted:

def conditional_minmax(df, xprefix, yprefix):

    new_df = (pd.wide_to_long(df.reset_index(),
                              stubnames=[xprefix, yprefix],
                              i='index',
                              j='xy')
              .reset_index()
              .drop('xy', axis=1)
              .groupby(['index', yprefix])[xprefix].agg(['max', 'min'])
              .groupby('index')
              .apply(lambda x: pd.Series(x.values[[0, -1], [1, 0]],
                                         index=['_xmin', '_xmax']))
              )

    new_df['_xidxmin'] = abs(df.filter(regex='^' + xprefix).sub(new_df['_xmin'], axis=0)).idxmin(axis=1)
    new_df['_xidxmax'] = abs(df.filter(regex='^' + xprefix).sub(new_df['_xmax'], axis=0)).idxmin(axis=1)

    return new_df

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.

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