熊猫中groupby函数的行为

时间:2020-04-17 10:21:56

标签: python pandas-groupby

我有一个包含国际贸易统计数据的数据框,该数据框根据协调的关税代码分为四个层次。级别1是1,2或3。级别2有2位数字,依此类推,级别4有4位数字。

我想按三级的特定代码进行过滤,并查看数据中所有四级项的分组汇总

filCode=df['Level3Code']=='242'
[filCode].groupby('Level4')['Total Exports'].sum().sort_values(ascending=False)
I'm expecting to see the following level four codes together with totals value sums for each code

'2421 Pharmaceutical products'      sum for this category
 '2422 Essential oils perfume & cosmetic products'    sum for this category
 '2423 Plastics & articles of plastic'     sum for this category
 '2424 Other chemical & chemical preparations elaborately transformed'    sum for this category
 '2425 Other semi-manufactures elaborately transformed   sum for this category

相反,我看到的是按照应该被过滤掉的类别计算的一堆总计...。因此:


T4
2421 Pharmaceutical products                                           3.983
2424 Other chemical & chemical preparations elaborately transformed    2.084
2422 Essential oils perfume & cosmetic products                        1.068
2423 Plastics & articles of plastic                                    0.742
2425 Other semi-manufactures elaborately transformed                   0.393
                                                                        ...  
2313 Non-ferrous metals simply transformed                             0.000
2312 Iron & steel simply transformed                                   0.000
2311 Non-metallic mineral manufactures simply transformed              0.000
3021 Non-monetary gold                                                 0.000
1111 Live animals chiefly for food                                     0.000
Name: Total Exports, Length: 61, dtype: float64

0 个答案:

没有答案