所以我有一个数据框 milk_countries_exports,它由以下列组成:
- “期间”,特定行的年份和月份(数据集是一年中的逐月)
- 正在做出口的“记者”国家
- 从“报告者”进口的“伙伴”国家
- “商品”由 2 项组成,“既不浓缩也不加糖的牛奶和奶油”,以及“浓缩或加糖的牛奶和奶油”
- “商品代码”,是分配给商品类型的编号,因此分别为“0401”和“0402”
- “贸易价值(美元)”,该行的给定月份、国家/地区和商品类型发生的贸易额
我被要求:
过滤数据集,使其仅包含总计满足以下条件的行:
特定国家所有奶制品的出口额为
在任何给定的每月期间至少有 200 万美元。 (提示:分组
合作伙伴和期间,并针对测试的函数进行过滤
最低贸易价值超过要求的价值。)
对我来说,这似乎意味着对于每个给定的月份和合作伙伴,如果该月(例如 201401)和合作伙伴(例如德国)的 0401 和 0402 之和大于 200 万美元,则保留这两行,否则将其过滤掉。因此,如果对于一对月份伴侣,0401 和 0402 的值分别为 900,000 和 2,000,000,则总和为 2,900,000,因此两行都会保留,即使其中一行小于 200 万阈值,因为它超出了0401 和 0402 的总和,而不是它们各自的值。
作为数据中的参考点,我使用了“德国”的前几行,因为作为合作伙伴,它包含 0401 和 0402 的示例(许多合作伙伴只有一个或另一个),并且至关重要的是,第一个月包含一个案例0401 低于 200 万阈值(982240),0402 为 3187636,超过阈值。因此,如果过滤后其中一行或两行丢失,我就知道我做错了什么。
我尝试以这种方式应用它:
我定义了一个函数来测试数据集是否满足大于或等于 200 万的标准:
def groupsWithExportsOver2million (g):
return g['Trade Value (US$)'].min() >= 2000000
Then:
milk_countries_exports[milk_countries_exports['Partner'] == 'Germany'].groupby(['Period', 'Partner']).filter(groupsWithExportsOver2million)
这会产生一个完全空白的数据框,并且:
milk_countries_exports[milk_countries_exports['Period'] == '201401'].groupby(['Period', 'Partner']).filter(groupsWithExportsOver2million)
做同样的事。然而:
milk_countries_exports.groupby(['Period', 'Partner']).filter(groupsWithExportsOver2million).sort_values('Trade Value (US$)')
(即显示整个数据集,而不仅仅是仅显示一个国家或时期组)会生成一个 56 行的数据框,其中仅包含超过 200 万行的“贸易价值(美元)”行,我认为但我不确定,它的过滤方式是,如果任何伙伴国家或任何时期值在数据框中的任何位置的贸易价值小于 200 万,则整个国家或时期的值将从结果数据框中消除。或者,换句话说,留下一个仅包含每个实例中贸易价值高于 200 万的国家和月份的表格。
我可以组合每个国家/时期对的 0401 和 0402 的值,并根据过滤器测试该总和,但这当然会返回一个数据帧,其中 0401 和 0402 组合为一个值,而实际上我想要做的的目的是能够对它们求和,测试过滤器,然后将它们分开,这样 0401 和 0402 仍然单独存在。
我该如何做我想做的事?
Data:
# copy data and create dataframe with
df = pd.read_clipboard(sep=',')
Period,Reporter,Partner,Commodity,Commodity Code,Trade Value (US$)
2014-09-01,United Kingdom,World,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,33279381
2014-09-01,United Kingdom,Australia,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,4558
2014-09-01,United Kingdom,Austria,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,290
2014-09-01,United Kingdom,Barbados,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,3016
2014-09-01,United Kingdom,Belgium,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,578297
2014-09-01,United Kingdom,Br. Virgin Isds,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,33746
2014-09-01,United Kingdom,Canada,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,34657
2014-09-01,United Kingdom,China,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1547888
2014-09-01,United Kingdom,Cyprus,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,20199
2014-09-01,United Kingdom,Denmark,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,89685
2014-09-01,United Kingdom,Falkland Isds (Malvinas),Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,35560
2014-09-01,United Kingdom,Finland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,141693
2014-09-01,United Kingdom,France,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,599127
2014-09-01,United Kingdom,Germany,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,587925
2014-09-01,United Kingdom,Gibraltar,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,29503
2014-09-01,United Kingdom,Greece,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,55
2014-09-01,United Kingdom,Guyana,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,74259
2014-09-01,United Kingdom,"China, Hong Kong SAR",Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,73082
2014-09-01,United Kingdom,Hungary,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,4141
2014-09-01,United Kingdom,Ireland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,28619275
2014-09-01,United Kingdom,Italy,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,11989
2014-09-01,United Kingdom,Japan,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,3337
2014-09-01,United Kingdom,Lithuania,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,275
2014-09-01,United Kingdom,Malta,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,3756
2014-09-01,United Kingdom,"Other Asia, nes",Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,5040
2014-09-01,United Kingdom,Netherlands,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,406087
2014-09-01,United Kingdom,Portugal,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,3939
2014-09-01,United Kingdom,Qatar,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,10796
2014-09-01,United Kingdom,Saint Helena,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,3323
2014-09-01,United Kingdom,Singapore,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,10254
2014-09-01,United Kingdom,Viet Nam,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,10529
2014-09-01,United Kingdom,Slovenia,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,11
2014-09-01,United Kingdom,South Africa,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,8067
2014-09-01,United Kingdom,Spain,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,210956
2014-09-01,United Kingdom,Trinidad and Tobago,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,9680
2014-09-01,United Kingdom,United Arab Emirates,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,37348
2014-09-01,United Kingdom,United States of America,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,67035
2014-05-01,United Kingdom,World,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,21665661
2014-05-01,United Kingdom,Denmark,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,2311364
2014-05-01,United Kingdom,France,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1638838
2014-05-01,United Kingdom,Germany,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,2199671
2014-05-01,United Kingdom,Italy,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,9950
2014-09-01,United Kingdom,World,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,27320918
2014-09-01,United Kingdom,Algeria,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,1659167
2014-09-01,United Kingdom,Angola,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,923506
2014-09-01,United Kingdom,Austria,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,443
2014-09-01,United Kingdom,Bangladesh,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,895167
2014-09-01,United Kingdom,Barbados,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,2027
2014-09-01,United Kingdom,Belgium,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,83721
2014-09-01,United Kingdom,Cameroon,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,83356
2014-09-01,United Kingdom,Sri Lanka,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,15332
2014-09-01,United Kingdom,China,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,1460603
2014-09-01,United Kingdom,Congo,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,673852
2014-09-01,United Kingdom,Cuba,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,849676
2014-09-01,United Kingdom,Cyprus,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,13521
2014-09-01,United Kingdom,Benin,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,112444
2014-09-01,United Kingdom,Denmark,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,63293
2014-09-01,United Kingdom,Dominican Rep.,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,535019
2014-09-01,United Kingdom,Finland,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,209
2014-09-01,United Kingdom,France,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,262833
2014-09-01,United Kingdom,Germany,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,2921594
2014-09-01,United Kingdom,Greece,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,52
2014-09-01,United Kingdom,Guinea,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,441798
2014-09-01,United Kingdom,Guyana,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,65483
2014-09-01,United Kingdom,"China, Hong Kong SAR",Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,1210921
2014-09-01,United Kingdom,Hungary,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,28794
2014-09-01,United Kingdom,Iraq,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,111141
2014-09-01,United Kingdom,Ireland,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,5498955
2014-09-01,United Kingdom,Italy,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,4204
2014-09-01,United Kingdom,Côte d'Ivoire,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,2322223
2014-09-01,United Kingdom,Jamaica,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,131078
2014-09-01,United Kingdom,Kuwait,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,623076
2014-09-01,United Kingdom,Lebanon,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,388137
2014-09-01,United Kingdom,Latvia,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,37
2014-09-01,United Kingdom,Malaysia,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,284043
2014-09-01,United Kingdom,Mali,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,220000
2014-09-01,United Kingdom,Malta,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,12190
2014-09-01,United Kingdom,Mauritania,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,73724
2014-09-01,United Kingdom,Mauritius,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,108406
2014-09-01,United Kingdom,Netherlands,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,979298
2014-09-01,United Kingdom,Nigeria,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,308385
2014-09-01,United Kingdom,Norway,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,5736
2014-09-01,United Kingdom,Pakistan,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,8151
2014-09-01,United Kingdom,Panama,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,142387
2014-05-01,United Kingdom,Lithuania,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,9354
2014-05-01,United Kingdom,Netherlands,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,664032
2014-05-01,United Kingdom,Portugal,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,912
2014-05-01,United Kingdom,Romania,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,225281
2014-05-01,United Kingdom,Slovakia,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,45434
2014-05-01,United Kingdom,Sweden,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,64547
2014-05-01,United Kingdom,United Arab Emirates,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,6742
2014-05-01,United Kingdom,Belgium,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,2707248
2014-05-01,United Kingdom,Ireland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,11065926
2014-05-01,United Kingdom,Poland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,716363
2014-10-01,United Kingdom,World,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,26615557
2014-10-01,United Kingdom,Austria,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,745
2014-10-01,United Kingdom,Belgium,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,609637
2014-10-01,United Kingdom,Bermuda,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1439
2014-10-01,United Kingdom,Canada,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,27639
2014-10-01,United Kingdom,Sri Lanka,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,2230
2014-10-01,United Kingdom,China,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1493672
2014-10-01,United Kingdom,Cyprus,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,5509
2014-10-01,United Kingdom,Denmark,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,72569
2014-10-01,United Kingdom,Falkland Isds (Malvinas),Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,15189
2014-10-01,United Kingdom,Finland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,184498
2014-10-01,United Kingdom,France,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,658064
2014-10-01,United Kingdom,Germany,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,936431
2014-10-01,United Kingdom,Gibraltar,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,13367
2014-10-01,United Kingdom,Guatemala,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,200251
2014-10-01,United Kingdom,"China, Hong Kong SAR",Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,265362
2014-10-01,United Kingdom,Hungary,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,11
2014-10-01,United Kingdom,Ireland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,21360068
2014-10-01,United Kingdom,Italy,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,12806
2014-10-01,United Kingdom,Japan,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,2127
2014-10-01,United Kingdom,Rep. of Korea,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,18549
2014-10-01,United Kingdom,Luxembourg,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,58
2014-10-01,United Kingdom,Malta,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,14189
2014-10-01,United Kingdom,Netherlands,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,337973
2014-10-01,United Kingdom,Portugal,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,3142
2014-10-01,United Kingdom,Singapore,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,8653
2014-10-01,United Kingdom,Viet Nam,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,10406
2014-10-01,United Kingdom,South Africa,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,7512
2014-10-01,United Kingdom,Spain,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,195426
2014-10-01,United Kingdom,United Arab Emirates,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,37325
2014-10-01,United Kingdom,Ukraine,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,2587
2014-10-01,United Kingdom,United States of America,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,118121
2014-07-01,United Kingdom,World,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,39508127
2014-07-01,United Kingdom,Afghanistan,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,4017
2014-07-01,United Kingdom,Austria,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,365
2014-07-01,United Kingdom,Bahrain,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,3348
2014-07-01,United Kingdom,Belgium,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,842327
2014-07-01,United Kingdom,Bermuda,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,2062
2014-07-01,United Kingdom,Br. Virgin Isds,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,39168
2014-07-01,United Kingdom,Canada,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,33987
2014-07-01,United Kingdom,Sri Lanka,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,3170
2014-07-01,United Kingdom,China,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1178497
2014-07-01,United Kingdom,Cyprus,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,21465
2014-07-01,United Kingdom,Denmark,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,111562
2014-07-01,United Kingdom,Falkland Isds (Malvinas),Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,23319
2014-07-01,United Kingdom,Finland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,135463
2014-07-01,United Kingdom,France,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,3606510
2014-07-01,United Kingdom,Germany,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,898346
2014-07-01,United Kingdom,Gibraltar,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,8565
2014-07-01,United Kingdom,Greece,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1743
2014-07-01,United Kingdom,"China, Hong Kong SAR",Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,665185
2014-07-01,United Kingdom,Hungary,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,8635
2014-07-01,United Kingdom,Ireland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,29943028
2014-07-01,United Kingdom,Italy,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,14349
2014-07-01,United Kingdom,Japan,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,7631
2014-07-01,United Kingdom,Rep. of Korea,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,2489
2014-07-01,United Kingdom,Malta,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,23238
2014-07-01,United Kingdom,Netherlands,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1424401
2014-07-01,United Kingdom,Portugal,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,9260
2014-07-01,United Kingdom,Singapore,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,7865
2014-07-01,United Kingdom,Viet Nam,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,11085
2014-07-01,United Kingdom,South Africa,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,9104
2014-07-01,United Kingdom,Spain,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,311615
2014-07-01,United Kingdom,Sweden,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,792
2014-07-01,United Kingdom,Switzerland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1444
2014-07-01,United Kingdom,United Arab Emirates,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,25733
2014-07-01,United Kingdom,United States of America,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,128358
2014-06-01,United Kingdom,World,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,16022428
2014-06-01,United Kingdom,Belgium,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1346169
2014-06-01,United Kingdom,Denmark,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1972893
2014-06-01,United Kingdom,France,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1449614
2014-06-01,United Kingdom,Germany,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,1856919
2014-06-01,United Kingdom,Ireland,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,7597407
2014-06-01,United Kingdom,Italy,Milk and cream; not concentrated nor containing added sugar or other sweetening matter,0401,8011
2014-09-01,United Kingdom,Poland,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,528311
2014-09-01,United Kingdom,Portugal,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,4418
2014-09-01,United Kingdom,Saint Lucia,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,11489
2014-09-01,United Kingdom,Saudi Arabia,Milk and cream; concentrated or containing added sugar or other sweetening matter,0402,715791
尝试解决方案时出现错误消息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-131-1bce8906203d> in <module>
----> 1 df_filtered = milk_countries_exports.groupby(['Partner', pd.Grouper(key='Period', freq='1M')]).filter(lambda x: x['Trade Value (US$)'].sum() > 2000000)
~\anaconda3\lib\site-packages\pandas\core\frame.py in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, observed)
5808 group_keys=group_keys,
5809 squeeze=squeeze,
-> 5810 observed=observed,
5811 )
5812
~\anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in __init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, observed, mutated)
407 sort=sort,
408 observed=observed,
--> 409 mutated=self.mutated,
410 )
411
~\anaconda3\lib\site-packages\pandas\core\groupby\grouper.py in get_grouper(obj, key, axis, level, sort, observed, mutated, validate)
623 in_axis=in_axis,
624 )
--> 625 if not isinstance(gpr, Grouping)
626 else gpr
627 )
~\anaconda3\lib\site-packages\pandas\core\groupby\grouper.py in __init__(self, index, grouper, obj, name, level, sort, observed, in_axis)
294 # check again as we have by this point converted these
295 # to an actual value (rather than a pd.Grouper)
--> 296 _, grouper, _ = self.grouper._get_grouper(self.obj, validate=False)
297 if self.name is None:
298 self.name = grouper.result_index.name
~\anaconda3\lib\site-packages\pandas\core\resample.py in _get_grouper(self, obj, validate)
1409 def _get_grouper(self, obj, validate: bool = True):
1410 # create the resampler and return our binner
-> 1411 r = self._get_resampler(obj)
1412 r._set_binner()
1413 return r.binner, r.grouper, r.obj
~\anaconda3\lib\site-packages\pandas\core\resample.py in _get_resampler(self, obj, kind)
1402
1403 raise TypeError(
-> 1404 "Only valid with DatetimeIndex, "
1405 "TimedeltaIndex or PeriodIndex, "
1406 f"but got an instance of '{type(ax).__name__}'"
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'