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.
>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