gpt4 book ai didi

python - 获取组开始和结束 pandas 的索引

转载 作者:行者123 更新时间:2023-12-01 09:15:41 25 4
gpt4 key购买 nike

我按月对数据进行分组。现在我需要知道我的小组从哪个观察/索引开始和结束。我得到的是以下输出,其中第二列代表每个月的观察次数:

date
01 145
02 2232
03 12785
04 16720
Name: date, dtype: int64

使用此代码:

leave.groupby([leave['date'].dt.strftime('%m')])['date'].count()

我想要的是一个我可以稍后访问的索引范围。不知怎的(格式并不重要,我不介意它返回列表还是数据框)

date
01 0 - 145
02 146 - 2378
03 2378 - 15163
04 15164 - 31884

最佳答案

尝试以下操作 - 使用shift

df['data'] = df['data'].shift(1).add(1).fillna(0).apply(int).apply(str) + ' - ' + df['data'].apply(str)

输出:

     data
date
1 0 - 145
2 146 - 2232
3 2233 - 12785
4 12786 - 16720
5 16721 - 30386
6 30387 - 120157

关于python - 获取组开始和结束 pandas 的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51287665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com