Home > Blockchain >  Python Pandas Flag for min max Values
Python Pandas Flag for min max Values

Time:08-27

I have a dataframe which looks like this:

import pandas as pd

    
d = {'Para1': ['Para1_1', 'Para1_1', 'Para1_2', 'Para1_2'], 'Para2': ['Para2_1', 'Para2_1', 'Para2_2', 'Para2_2'], 'ParaN': ['ParaN_1', 'ParaN_1', 'ParaN_2', 'ParaN_2'],'value':[0.5,0.3,0.01,0.5]}

df=pd.DataFrame(data=d)

print (df)

output:

     Para1    Para2    ParaN  value
0  Para1_1  Para2_1  ParaN_1   0.50
1  Para1_1  Para2_1  ParaN_1   0.30
2  Para1_2  Para2_2  ParaN_2   0.01
3  Para1_2  Para2_2  ParaN_2   0.50

For each Combination of Para1 Para2 ParaN a have two values. I need a flag which is the max and which is the min value of the combination. So that it looks like this:

     Para1    Para2    ParaN  value   minmaxflag
0  Para1_1  Para2_1  ParaN_1   0.50    max
1  Para1_1  Para2_1  ParaN_1   0.30    min
2  Para1_2  Para2_2  ParaN_2   0.01    min
3  Para1_2  Para2_2  ParaN_2   0.50    max

I have tried differnt things. But nothing seems to be working into the right direction.

Best Zio

CodePudding user response:

You can use .replace:

params = ['Para1', 'Para2', 'ParaN']

df['minmaxflag'] = df.groupby(params)['value'].transform(lambda s: s.replace({s.min(): 'min', s.max(): 'max'}))

Result:

print(df)
     Para1    Para2    ParaN  value minmaxflag
0  Para1_1  Para2_1  ParaN_1   0.50        max
1  Para1_1  Para2_1  ParaN_1   0.30        min
2  Para1_2  Para2_2  ParaN_2   0.01        min
3  Para1_2  Para2_2  ParaN_2   0.50        max

CodePudding user response:

If you only have a min/max per pairing this will give you the answers your looking for

import pandas as pd
import numpy as np

d = {'Para1': ['Para1_1', 'Para1_1', 'Para1_2', 'Para1_2'], 'Para2': ['Para2_1', 'Para2_1', 'Para2_2', 'Para2_2'], 'ParaN': ['ParaN_1', 'ParaN_1', 'ParaN_2', 'ParaN_2'],'value':[0.5,0.3,0.01,0.5]}

df=pd.DataFrame(data=d)
df['minmaxflag'] = df.groupby(['Para1', 'Para2', 'ParaN'])['value'].transform('max')
df['minmaxflag'] = np.where(df['value'] == df['minmaxflag'], 'max', 'min')
df
  • Related