Python-我想每隔10年group_by并合并回去

时间:2019-04-01 14:16:20

标签: python pandas intervals

我的数据如下:

Pkey    CalenderYear
1   1996
2   2005
3   2010
4   2019
5   1985

我想用

按十年间隔对它进行分类
decade = 10 * (df_CNratio['CalenderYear'] // 10)
decade = decade.astype(str) + 's'
decade.name = 'decade'

My desired outcome:

Pkey  CalenderYear Interval
1      1996         1990
2      2005         2000
3      2010         2010
4      2019         2010
5      1985         1980

在大熊猫中怎么做?

1 个答案:

答案 0 :(得分:2)

只需将您的解决方案分配到新列:

df['Interval'] = (10 * (df['CalenderYear'] // 10)).astype(str) + 's'
print (df)
   Pkey  CalenderYear Interval
0     1          1996    1990s
1     2          2005    2000s
2     3          2010    2010s
3     4          2019    2010s
4     5          1985    1980s