Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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 如何使用Pandas count包含零值并将结果与原始数据帧合并_Python_Pandas - Fatal编程技术网

Python 如何使用Pandas count包含零值并将结果与原始数据帧合并

Python 如何使用Pandas count包含零值并将结果与原始数据帧合并,python,pandas,Python,Pandas,我的数据集如下所示: Year Month Day Category Quantity 1984 1 1 2 10.5 1984 1 1 6 3.7 1985 1 2 8 4.8 1985

我的数据集如下所示:

   Year     Month   Day         Category          Quantity
   1984     1        1          2                   10.5
   1984     1        1          6                   3.7
   1985     1        2          8                   4.8
   1985     2        1          3                   20
   1986     1        1          1                   9
   1986     2        1          18                  12.6
   1987     1        29         20                  2.8
请注意,每年每个月的每一天都包含一个唯一的条目。换句话说,每天只能有一个类别(不是几个)

我试图计算每年每个类别发生的次数

然而,使用Pandas中的count,我意识到不包括零计数。 换言之,如果某一类别在一年内未发生,则不包括在内。为了解决这个问题,我尝试使用:
fill\u value=0
(如下代码所示)

我的结论是(警告:不要按原样运行此代码,因为它显然会占用所有内存):

dataframe df的数据类型在运行该代码之后:

The datatypes for the dataframe that is imported from the CSV is as:

Year                int64
Month               int64
Day                 int64
Category            int64
Quantity            object
QuantityWithNaN     float64
因此,最终结果应该是类似的,但不是用上面的代码实现的。(最终结果不能按任何特定顺序排序,唯一重要的是每年都会出现所有类别):

此外,对于可视化和最终重要的信息,将完全由以下列给出,因此包含零计数,并且每年和类别的每个组合都没有重复的行(显然我很懒,每年在这里包含所有类别(1-20)需要更多的空间):

现在,我得到了一个单独的
系列对象
count\u quantity\u yearly\u高于\u 5
),我想将其插入原始的
数据帧
df

使用
reindex
我想减少行数,这样每年和类别的唯一组合只有一行,这意味着每年和类别的每个组合只出现一次(换句话说,每年每个类别只显示一次)

显然,
fill\u value=0
应该告诉pandas
count
包含零计数

显然,代码有很大的问题,因为运行它时会消耗所有内存,我怀疑这是由于代码中的以下行之一:

count_quantity_yearly_above_5.reindex(midx, fill_value=0)

df['count_quantity_yearly_above_5'] = df.apply(count_quantity_yearly_above_5,axis=1)
编辑

主要问题是,我无法将
count\u quantity\u yearly\u over_5
-列放入原始数据帧,这可能与
count\u quantity\u yearly\u over_5
是一个系列对象这一事实有关。现在我显然没有正确地将series对象导入原始数据帧。 对如何调整此代码有何建议

仅运行行(
df['count\u quantity\u yearly\u over\u 5']=df.apply(count\u quantity\u yearly\u over\u 5,axis=1)
)返回错误:

TypeError: ("'Series' object is not callable", 'occurred at index 0')
编辑2

我刚刚发现哪一行导致了100%的内存使用率:

count_quantity_yearly_above_5.reindex(midx, fill_value=0)

您可能需要使用
分组方式


下面将返回您想要的最终表格。仅包括列
年度
类别
数量_年度_5以上

df.groupby(['Year', 'Category']).size().reset_index(name='count_quantity_yearly_above_5')

那么,我是否只将代码的最后一行替换为您发布的代码?它返回一个错误,表示未定义高于5的
count\u quantity\u yearly\u
请在读取csv后立即尝试。如果它能工作,你就不需要剩下的代码了。只要读入csv并运行该行。您可能希望将其指定为数据帧,即
df=df.groupby(…
)。奇怪的是,您得到了这个错误,因为该列不需要预定义。
TypeError: ("'Series' object is not callable", 'occurred at index 0')
count_quantity_yearly_above_5.reindex(midx, fill_value=0)
df.groupby(['Year', 'Category']).size().reset_index(name='count_quantity_yearly_above_5')