Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何计算分类值(包括零出现)?_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何计算分类值(包括零出现)?

Python 如何计算分类值(包括零出现)?,python,pandas,dataframe,Python,Pandas,Dataframe,我想按月计算代码的数量。 这是我的示例数据帧 id month code 0 sally 0 s_A 1 sally 0 s_B 2 sally 0 s_C 3 sally 0 s_D 4 sally 0 s_E 5 sally 0 s_A 6 sally 0 s_A 7 sally 0 s_B 8 sally

我想按月计算代码的数量。 这是我的示例数据帧

        id    month  code
0     sally    0  s_A
1     sally    0    s_B
2     sally    0   s_C
3     sally    0   s_D
4     sally    0    s_E
5     sally    0   s_A
6     sally    0    s_A
7     sally    0   s_B
8     sally    0   s_C
9     sally    0   s_A
我使用count()转换到这个系列

但是,我想包括零发生率,像这样

id      code   month  count
sally  s_A      0    12
                1    10
                2     3
                3    0
                4    0
                5    0
                6    0
                7    15
                8    0
                9    0
                10   0
                11   0
您可以与新建的
Multindex
一起使用:

注意:


旧的解决方案:

使用and重新索引
,但随后需要一些数据清理:

df = df.groupby(['id', 'code', 'month']).size() \
       .to_frame('count') \
       .unstack([0,1], fill_value=0) \
       .reindex(range(13), fill_value=0) \
       .stack([1,2], dropna=False) \
       .fillna(0) \
       .astype(int) \
       .swaplevel(0,2) \
       .sort_index()
print (df)
                   count
code id     month       
s_A  sally  0          1
            1          3
            2          0
            3          0
            4          0
            5          0
            6          0
            7          0
            8          0
            9          0
            10         0
            11         0
            12         0
     sally1 0          3
            1          0
            2          0
            3          0
            4          0

您没有显示
'm'
column@splinter我更新了。m是月。Mux是很好的解决方案。你救了我一天!
df = pd.DataFrame({
'month': [0, 0, 0, 0, 1, 1, 1, 2, 2, 7], 
'code': ['s_A', 's_A', 's_A', 's_A', 's_A', 's_A', 's_A', 's_B', 's_B', 's_B'], 
'id': ['sally1','sally1','sally1','sally','sally','sally','sally','sally','sally','sally']})

print (df)
  code      id  month
0  s_A  sally1      0
1  s_A  sally1      0
2  s_A  sally1      0
3  s_A   sally      0
4  s_A   sally      1
5  s_A   sally      1
6  s_A   sally      1
7  s_B   sally      2
8  s_B   sally      2
9  s_B   sally      7
df = df.groupby(['id', 'code', 'month']).size()
n = ['id','code','month']
mux = pd.MultiIndex.from_product([df.index.levels[0],df.index.levels[1], range(13)], names=n)
df = df.reindex(mux, fill_value=0)
print (df)
id      code  month
sally   s_A   0        1
              1        3
              2        0
              3        0
              4        0
              5        0
              6        0
              7        0
              8        0
              9        0
              10       0
              11       0
              12       0
        s_B   0        0
              1        0
              2        2
              3        0
...
...
df = df.groupby(['id', 'code', 'month']).size() \
       .to_frame('count') \
       .unstack([0,1], fill_value=0) \
       .reindex(range(13), fill_value=0) \
       .stack([1,2], dropna=False) \
       .fillna(0) \
       .astype(int) \
       .swaplevel(0,2) \
       .sort_index()
print (df)
                   count
code id     month       
s_A  sally  0          1
            1          3
            2          0
            3          0
            4          0
            5          0
            6          0
            7          0
            8          0
            9          0
            10         0
            11         0
            12         0
     sally1 0          3
            1          0
            2          0
            3          0
            4          0