简体   繁体   English

从 OrderedDict 中提取数据

[英]Extract Data from OrderedDict

I would like to remove data from OrderedDict.我想从 OrderedDict 中删除数据。

I have the following list:我有以下清单:

1384   2018-11-23
1385   2018-11-23
1386   2018-11-23
1387   2018-11-23
1397   2018-11-24
1398   2018-11-24
1399   2018-11-24
1400   2018-11-24
1401   2018-11-24
1402   2018-11-24
1403   2018-11-24
1404   2018-11-24
1417   2018-11-25
1418   2018-11-25
1419   2018-11-25
1420   2018-11-25
1421   2018-11-25
1422   2018-11-25
1423   2018-11-25
1424   2018-11-25
1425   2018-11-25
1426   2018-11-25
1427   2018-11-25
1428   2018-11-25
1439   2018-11-25
1440   2018-11-26
1441   2018-11-26
1442   2018-11-26
1443   2018-11-26
1444   2018-11-26
1445   2018-11-26
1446   2018-11-26
1447   2018-11-26
1448   2018-11-26
1449   2018-11-26
1450   2018-11-26
1452   2018-11-26
1461   2018-11-26
1462   2018-11-26
1463   2018-11-26
1464   2018-11-27
1465   2018-11-27
1466   2018-11-27
1467   2018-11-27
1468   2018-11-27
1670   2018-12-05
1671   2018-12-05
1686   2018-12-06
1687   2018-12-06
1688   2018-12-06

I've already counted the occurrence of each date with this code:我已经用这个代码计算了每个日期的出现次数:

inp=df_nan[label_date]
odct = OrderedDict()
for item in inp:
    try:
        odct[item] += 1
    except KeyError:
        odct[item] = 1

It gives me the output:它给了我输出:

OrderedDict
([(Timestamp('2018-11-23 00:00:00'), 4),

(Timestamp('2018-11-24 00:00:00'), 8),

(Timestamp('2018-11-25 00:00:00'), 13),

(Timestamp('2018-11-26 00:00:00'), 15),

(Timestamp('2018-11-27 00:00:00'), 5),

(Timestamp('2018-12-05 00:00:00'), 2),

(Timestamp('2018-12-06 00:00:00'), 3)])

Now I would like to keep only the dates that's equal or below 5.现在我只想保留等于或小于 5 的日期。

Once it's done.一旦完成。 I would like to remove the dates in my dataset where this occurence is higher than 5.我想删除数据集中出现次数高于 5 的日期。

Thanks for your help.谢谢你的帮助。

您可以使用内置函数filter

filter(lambda x: odct[x] <= 5, df_nan)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM