Q:

how to sort the chunk of list in python?

I have a list of rates, which contain almost 35040 values in it. I have divided my list into 365 blocks of 96 elements in it. Now I want to get the first 4 minimum values from each block and to achieve that first I am sorting blocks in increasing order and printing or inserting the first 4 elements from the list into a new list.
my approach:
import pandas as pd

inputFile = "inputFile.xlsx"
fileName = inputFile
inputSheetDF = pd.read_excel(fileName, sheet_name='Sheet1')

iexRate = inputSheetDF['IEX Price']
#iexRate = [2.3, 2.4, 3, 4, 3.2, 4.1, 5.......]

testList = []

n = 96

x = [iexRate[i:i + n] for i in range(0, len(iexRate), n)]
x.sort()

but this x.sort() giving me an error.
ValueError: Can only compare identically-labeled Series objects

So basically I want an output in that testList which contains the first 4 minimum elements in each 96 block.

A:

Here's a proposed solution, which has the advantage of being vectorized. I'm using a much smaller dataset - 3 chunks of 4 each, sampling the top (button) 2 from each chunk - but the idea for a larger dataset is of course the same. 
df = pd.DataFrame({"rate": np.random.randint(1, 100, 12), "chunk":  [1]*4 + [2]*4 + [3]*4 })
print(df)

==> 

    rate  chunk
0     81      1
1     51      1
2     50      1
3     83      1
4     33      2
5     88      2
6     97      2
7      2      2
8     22      3
9     23      3
10     4      3
11    83      3

df.sort_values("rate", inplace=True)
df.groupby("chunk").head(2).sort_values("chunk")

==>

    rate  chunk
2     50      1
1     51      1
7      2      2
4     33      2
10     4      3
8     22      3

To get a flat list of all the rates, just do : 
flat_list = list(res.rate)
==> [50, 51, 2, 33, 4, 22]

A:

iexRate = pd.Series(range(1,100))

n = 15

x = [iexRate[i:i + n] for i in range(0, len(iexRate), n)]

testList = [sorted(block)[:4] for block in x]

[[1, 2, 3, 4], [16, 17, 18, 19], [31, 32, 33, 34], [46, 47, 48, 49], [61, 62, 63, 64], [76, 77, 78, 79], [91, 92, 93, 94]]

