简体   繁体   English

为什么这些 Pandas groupby 上的差异会导致 Nan?

[英]Why does diff on these Pandas groupby results in Nan?

The example dataframe I have is-我的示例 dataframe 是-

>>> new_df
        date country  score
0 2018-01-01      ch     50
1 2018-01-01      es    100
2 2018-01-01      us    150
3 2018-01-02      ch     10
4 2018-01-02      gb    100
5 2018-01-02      us    125
6 2018-01-03      us    160

Why does new_df.groupby(["date", "country"]).diff() produce Nan?为什么new_df.groupby(["date", "country"]).diff()会产生 Nan?

>>> new_df.groupby(["date", "country"]).diff()
   score
0    NaN
1    NaN
2    NaN
3    NaN
4    NaN
5    NaN
6    NaN

As you can see the size of each group is 1 , then the subnetting of the subtraction is NaN because to make the subtraction a minuend and a subtraend are needed, that is to say size at least equal to 2 :如您所见,每个组的大小为1 ,那么减法的子网划分为NaN因为要使减法成为被减数和被减数,也就是说大小至少等于 2

df.groupby(['date','country']).size()

date        country
2018-01-01  ch         1
            es         1
            us         1
2018-01-02  ch         1
            gb         1
            us         1
2018-01-03  us         1
dtype: int64

It's because there is nothing to subtract--you have only one value per group in your example.这是因为没有什么可减去的——在您的示例中,每组只有一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM