Python - 迭代从数据帧中的列检索值

时间:2015-11-26 13:12:48

标签: python pandas dataframe

我与以下数据框一起列有DescriptionWeekCount列。

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

我想检索每个Description的最后5个计数并继续执行所有计数并对结果执行一些操作。换句话说,我想为每个唯一的描述值检索最近5 count的{​​{1}} Description。例如:对于Week,我的输出为 - N25846 External EMERGENCY STOP。同样,对于42, 114, 655, 922, 162,结果将是 - P873 ENCLOSURE DOOR CAN BE OPENED

2 个答案:

答案 0 :(得分:0)

您可以使用数据框的groupbytail方法:

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

答案 1 :(得分:0)

将上面的内容分配给变量df,您可以尝试以下内容:

    result = df.groupby('Description').apply(func)

其中func类似于:

    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()

如果不尝试此操作,我无法完全确定func的返回值是否可以与apply一起使用,因此您可能需要稍微使用它。但是这应该给你一个系列,索引是描述,值是最后五个计数的列表或词典。