df.groupby() 之后按照特定顺序输出,方便后续作图,或者跟其他df对比作图。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
## 构造 pd.dataframe patient_id = [ '71835318256532' , '87791375711' , '66979212649388' , '46569922967175' , '998612492555522' , '982293214194' , '89981833848' , '17912315786975' , '4683495482494' , '1484143378533' , '56866972273357' , '7796319285658' , '414462476158336' , '449519578512573' , '61826664459895' ] week = [ 'tuesday' , 'tuesday' , 'wednesday' , 'monday' , 'tuesday' , 'monday' , 'friday' , 'tuesday' , 'monday' , 'friday' , 'saturday' , 'thursday' , 'wednesday' , 'thursday' , 'wednesday' ] d = { 'patient_id' : patient_id, 'week' :week} test = pd.dataframe(data = d) ## 聚类计数 test.groupby( 'week' )[ 'patient_id' ].count() ## output week friday 2 monday 3 saturday 1 thursday 2 tuesday 4 wednesday 3 name: patient_id, dtype: int64 ## 按照特定顺序输出 ind = [ 'monday' , 'tuesday' , 'wednesday' , 'thursday' , 'friday' , 'saturday' ] test.groupby( 'week' )[ 'patient_id' ].count()[ind] ## output week monday 3 tuesday 4 wednesday 3 thursday 2 friday 2 saturday 1 name: patient_id, dtype: int64 |
作图效果如下
1
|
test.groupby( 'week' )[ 'patient_id' ].count().plot(kind = 'bar' ); |
1
2
|
ind = [ 'monday' , 'tuesday' , 'wednesday' , 'thursday' , 'friday' , 'saturday' ] test.groupby( 'week' )[ 'patient_id' ].count()[ind].plot(kind = 'bar' ); |
总结
以上所述是小编给大家介绍的pandas 按照特定顺序输出的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/Guo_ya_nan/article/details/80989067