'How to use nsmallest between a range in my dataframe?

I have a dataframe with a list of prices for every hour of the year. I need to get the nsmallest prices every 24 hours.

I have the following code but I can only use it for one day

for j in range(0,len(Winter_Data.index)+1):
    if ((j+1) % 24)==0:
        Qreheat= Winter_Data.loc[i:j,'Qflex'].sum()
        Heat_units = Qreheat/HP_heat_capacity
        hours = Qreheat/Heat_units
        Winter_Data.loc[Winter_Data.nsmallest(int(hours),'Pbill').index, 'Qreheat'] = Heat_units
        i=i+24
        print(Winter_Data['Qreheat'])

The dataframe should look like this: enter image description here

I want to set for instance 10 in the column Qreheat in the 2 nsmallest values of Pbill. The thing is that lets say I have two days. when I call nsmallest it takes the 2 nsmallest values of the whole column, but I only want the nsmallest of the last 24 rows.



Solution 1:[1]

Replace 'time_column' name with your time column. Also convert that column to datetime if it's not already, using

Winter_Data['time_column'] = pd.to_datetime(Winter_Data['time_column'])

and then, do this:

Winter_Data.resample('24H', on='time_column').agg({'Pbill':min})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1