Stack Exchange
Python - Iterating retrieval of values from a column in dataframe
I've the following dataframe with me with columns Description, Week, Count.

                                            Description       Week  count
527                      N25846 External EMERGENCY STOP 2015-05-03     77
528                      N25846 External EMERGENCY STOP 2015-05-10    983
529                      N25846 External EMERGENCY STOP 2015-05-17    245
530                      N25846 External EMERGENCY STOP 2015-05-24    897
531                      N25846 External EMERGENCY STOP 2015-05-31   2144
532                      N25846 External EMERGENCY STOP 2015-06-07    926
533                      N25846 External EMERGENCY STOP 2015-06-14   1082
534                      N25846 External EMERGENCY STOP 2015-06-21   1935
535                      N25846 External EMERGENCY STOP 2015-06-28   1511
536                      N25846 External EMERGENCY STOP 2015-07-05   3206
537                      N25846 External EMERGENCY STOP 2015-07-12   2031
538                      N25846 External EMERGENCY STOP 2015-07-19    590
539                      N25846 External EMERGENCY STOP 2015-07-26   1085
540                      N25846 External EMERGENCY STOP 2015-08-02    162
541                      N25846 External EMERGENCY STOP 2015-08-09    922
542                      N25846 External EMERGENCY STOP 2015-08-16    655
543                      N25846 External EMERGENCY STOP 2015-08-23    114
544                      N25846 External EMERGENCY STOP 2015-08-30     42
1747                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-05-03    274
1748                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-05-10   1358
1749                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-05-17   1047
1750                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-05-24    833
1751                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-05-31   1277
1752                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-06-07    812
1753                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-06-14    648
1754                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-06-21    666
1755                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-06-28    918
1756                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-07-05   1614
1757                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-07-12   1173
1758                  P873 ENCLOSURE DOOR CAN BE OPENED 2015-07-19    772

I want to retrieve the last 5 counts of each Description and continue it for all and perform some operation on the result. In other words, I want to retrieve count of Description for recent 5 Week for every unique description value. For example: for  N25846 External EMERGENCY STOP, my output would be - 42, 114, 655, 922, 162. Similarly, for P873 ENCLOSURE DOOR CAN BE OPENED, the result would be - 772, 1173, 1614, 918, 666

You could use groupby and tail methods of dataframes:
In [71]: df.groupby('Description').tail(5)
Out[71]:
    index                        Description        Week  count
13    540     N25846 External EMERGENCY STOP  2015-08-02    162
14    541     N25846 External EMERGENCY STOP  2015-08-09    922
15    542     N25846 External EMERGENCY STOP  2015-08-16    655
16    543     N25846 External EMERGENCY STOP  2015-08-23    114
17    544     N25846 External EMERGENCY STOP  2015-08-30     42
25   1754  P873 ENCLOSURE DOOR CAN BE OPENED  2015-06-21    666
26   1755  P873 ENCLOSURE DOOR CAN BE OPENED  2015-06-28    918
27   1756  P873 ENCLOSURE DOOR CAN BE OPENED  2015-07-05   1614
28   1757  P873 ENCLOSURE DOOR CAN BE OPENED  2015-07-12   1173
29   1758  P873 ENCLOSURE DOOR CAN BE OPENED  2015-07-19    772


With the above assigned to variable df you can try something like:
    result = df.groupby('Description').apply(func)

where func is something like:
    def func(group):
        # return the last five entries in the count column
        result = group['count'].iloc[-5:]
        # if you need the week data, use to_dict() instead of tolist()
        return result.tolist()

Without trying this I can't be entirely sure that the returned value of func will be acceptable for use with apply, so you might need to play around with that a little. But this should give you a series with the index being the description and the value being a list or dict of the last five counts.
