首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用months列进行装箱

使用months列进行装箱
EN

Stack Overflow用户
提问于 2019-10-24 19:48:35
回答 1查看 289关注 0票数 1
代码语言:javascript
复制
i have data frame which contains fields casenumber , count and credated date .here created date is months which are in numerical i want to make dataframe as arrenge the ranges to the count acoording to createddate column 

我在这里使用了下面的代码,但我不符合我的要求。我有包含字段casenumber,count和created date的数据框.here创建日期是以数字表示的月份,我想根据createddate列将数据框的范围扩大到count

代码语言:javascript
复制
i have data frame as below 
casenumber  count  CREATEDDATE
3820516     1      jan             
3820547     1      jan            
3820554     2      feb             
3820562     1      feb             
3820584     1      march             

4226616     1      april            
4226618     2      may             
4226621     2      may            
4226655     1      june            
4226663     1      june           

Here i used below code but i didnot match my requirement.i have data frame which contains fields casenumber , count and credated date .here created date is months which are in numerical i want to make dataframe as arrenge the ranges to the count acoording to createddate column


import pandas as pd
import numpy as np
df = pd.read_excel(r"")
bins = [0, 1 ,4,8,15, np.inf]
names = ['0-1','1-4','4-8','8-15','15+']
df1 = df.groupby(pd.cut(df['CREATEDDATE'],bins,labels=names))['casenumber'].size().reset_index(name='No_of_times_statuschanged')

    CREATEDDATE No_of_times_statuschanged
0   0-1               2092
1   1-4               9062
2   4-8               12578
3   8-15               3858
4   15+                 0

I got the above data as out put but my expected should be range for month on month based on the cases per month .
expected output should be like
CREATEDDATE    jan feb march april may june
   0-1          1   2   3     4     5   6
   1-4          3   0   6     7     8   9
   4-8          4    6  3     0     9    2
   8-15         0    3   4    5     8    9

我得到了以上的数据作为输出,但我的预期应该是每月的情况下,每月的范围。预期输出应如下所示

EN

回答 1

Stack Overflow用户

发布于 2019-10-24 19:57:33

crosstab与change CREATEDDATE to count for pd.cut一起使用,并通过列名列表逐个更改列的顺序:

代码语言:javascript
复制
#add another months if necessary
months = ["jan", "feb", "march", "april", "may", "june"]

bins = [0, 1 ,4,8,15, np.inf]
names = ['0-1','1-4','4-8','8-15','15+']
df1 = pd.crosstab(pd.cut(df['count'],bins,labels=names), df['CREATEDDATE'])[months]

print (df1)
CREATEDDATE  jan  feb  march  april  may  june
count                                         
0-1            2    1      1      1    0     2
1-4            0    1      0      0    2     0

另一个想法是使用ordered categoricals

代码语言:javascript
复制
df1 = pd.crosstab(pd.cut(df['count'],bins,labels=names), 
                  pd.Categorical(df['CREATEDDATE'], ordered=True, categories=months))

print (df1)
col_0  jan  feb  march  april  may  june
count                                   
0-1      2    1      1      1    0     2
1-4      0    1      0      0    2     0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58540748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档