2

I have a Dataframe with the index set to be the dates. I need to find the row (set of parameters for each month) where the value of the sensor is the minimum. The codes I tried are as following:

1.optimization = r.loc[[r.index.month == month]]  
  optimization=r.loc[r['Sensors'].idxmin()]
2.optimization=DataFrame(r.resample('M').Sensors.min()) 
3.optimization=r.loc[r['Sensors'].idxmin()]
4.optimization= r.loc[r.groupby([r.index.month == month])["Sensors"].idxmin()]

My datat farme:

Index          ACH_Base        Am          Energy                  Sensors  

2017_01_31       0.3           0.9         2.2989e+11          39087428391.1

2017_01_31       0.5           0.8         2.29892e+11         37142574944.9

....
2017_02_28       0.7           0.9         2.40001e+11         38568420286.9

2017_02_28       0.3           0.7         2.7136+11           36945759284.8

....

Thanks!

Maryam Nahid
  • 59
  • 1
  • 9

1 Answers1

0

You can use Resampler.transform for get minimal values per month, compare with Sensors and filter by boolean indexing:

df.index = pd.to_datetime(df.index, format='%Y_%m_%d')

df = df[df['Sensors'] == df.resample('M')['Sensors'].transform('min')]
print (df)
            ACH_Base   Am       Energy       Sensors
2017-01-31       0.5  0.8  2.29892e+11  3.714257e+10
2017-02-28       0.3  0.7    2.7136+11  3.694576e+10
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252