Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

min from columns from dict

I have a dict with item\column name and a df with columns from dict and other columns. How can I add column to df with min value for every item just from columns corresponding from dict?

import pandas as pd
my_dict={'Item1':['Col1','Col3'],
'Item2':['Col2','Col4']
        }
df=pd.DataFrame({
            'Col0':['Item1','Item2'],
            'Col1':[20,25],
            'Col2':[89,15],
            'Col3':[26,30],
            'Col4':[40,108],
            'Col5':[55,2]
            })
df['min']=?

I tried

df['min']=df[df.columns[df.columns.isin(my_dict)]].min(axis=1),

but it didn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use apply with a function that reads the appropriate column names out of the dictionary (returning an empty list if there is no match) and then takes the minimum of the specified columns:

my_dict = {
    'Item1': ['Col1', 'Col3'],
    'Item2': ['Col2', 'Col4']
}

df['min'] = df.apply(lambda r:r[my_dict.get(r['Col0'], [])].min(), axis=1)

Output:

    Col0  Col1  Col2  Col3  Col4  Col5  min
0  Item1    20    89    26    40    55   20
1  Item2    25    15    30   108     2   15

If it’s possible my_dict may contain column names that don’t exist in the dataframe, you can check for that in the function. For example:

my_dict = {
    'Item1': ['Col1', 'Col3'],
    'Item2': ['Col4', 'Col6']
}

df['min'] = df.apply(lambda r:r[[col for col in my_dict.get(r['Col0'], []) if col in r]].min(), axis=1)

Output:

    Col0  Col1  Col2  Col3  Col4  Col5  min
0  Item1    20    89    26    40    55   20
1  Item2    25    15    30   108     2  108
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading