Python 3.x Pandas groupby.pct_change()仅返回0和nan

Python 3.x Pandas groupby.pct_change()仅返回0和nan,python-3.x,pandas,pandas-groupby,Python 3.x,Pandas,Pandas Groupby,我有一个数据集: month country user 2018-01 DE 182503 2018-02 DE 168962 2018-03 DE 175043 2019-01 DE 158797 2019-01 DE 226209 我想看到连续行中的百分比变化,我使用pct_变化,但是我的输出只包含nan或0或-0。 我想创建一个

我有一个数据集:

month      country       user
2018-01    DE            182503
2018-02    DE            168962
2018-03    DE            175043
2019-01    DE            158797
2019-01    DE            226209
我想看到连续行中的百分比变化,我使用pct_变化,但是我的输出只包含nan或0或-0。 我想创建一个新列,pct_change并将百分比变化存储到其中

我的用户列是浮动类型

到目前为止,我的输出如下所示

month      country       user    pct_change
2018-01    DE            182503  nan
2018-02    DE            168962  -0
2018-03    DE            175043   0
我正在使用以下代码:

df['pct_change'] = df.groupby(['country'])['user'].pct_change()

为什么我没有看到正确的结果?

这只是一个取整问题吗

pd.options.display.float_format = '{:,.4f}'.format 
df['pct_change'] = df.groupby(['country'])['user'].pct_change()

df
Out[101]: 
     month country    user  pct_change
0  2018-01      DE  182503         NaN
1  2018-02      DE  168962     -0.0742
2  2018-03      DE  175043      0.0360

这只是一个取整问题吗

pd.options.display.float_format = '{:,.4f}'.format 
df['pct_change'] = df.groupby(['country'])['user'].pct_change()

df
Out[101]: 
     month country    user  pct_change
0  2018-01      DE  182503         NaN
1  2018-02      DE  168962     -0.0742
2  2018-03      DE  175043      0.0360

您的代码与您的output@user3483203:我使用了完全相同的代码。您的代码与您的不匹配output@user3483203:我使用了完全相同的代码。非常感谢!我太愚蠢了,以至于有一行代码提到了这一点:pd.set_option'display.float_format',lambda x:'%0.f'%x'。我想我只是迷路了,但再次感谢你!很高兴能为您提供帮助。我将在5分钟内接受答案,因为它不允许我在找到之前接受答案@非常感谢你!我太愚蠢了,以至于有一行代码提到了这一点:pd.set_option'display.float_format',lambda x:'%0.f'%x'。我想我只是迷路了,但再次感谢你!很高兴能为您提供帮助。我将在5分钟内接受答案,因为它不允许我在找到之前接受答案@索菲德