Zero occurrences/frequency using value_counts() in PANDAS

前端 未结 2 1545
我寻月下人不归
我寻月下人不归 2021-01-08 00:16

I have a table containing dates and the various cars sold on each dates in the following format (These are only 2 of many columns):

DATE       CAR
2012/01/01         


        
2条回答
  •  臣服心动
    2021-01-08 00:24

    The default behavior of type category is exactly what you want. The non present categories will display with a value of zero. You just need to do:

    df.astype({'CAR': 'category'})[df.CAR=='BMW']['DATE'].value_counts()
    

    or better yet, make it definitively a category in your dataframe:

    df.CAR = df.CAR.astype('category')
    df[df.CAR=='BMW'].DATE.value_counts()
    

    The category type is a better representation of your data and more space-efficient.

提交回复
热议问题