Mar*_*urg 29 python dataframe pandas
如果我做
mt = mobile.PattLen.value_counts() # sort True by default
Run Code Online (Sandbox Code Playgroud)
我明白了
4 2831
3 2555
5 1561
[...]
Run Code Online (Sandbox Code Playgroud)
如果我做
mt = mobile.PattLen.value_counts(sort=False)
Run Code Online (Sandbox Code Playgroud)
我明白了
8 225
9 120
2 1234
[...]
Run Code Online (Sandbox Code Playgroud)
我想要做的是以2,3,4的升序(左数字列)获得输出.我可以以某种方式更改value_counts还是需要使用其他函数.
jez*_*ael 69
我认为你需要sort_index,因为左栏被调用index.完整的命令将是mt = mobile.PattLen.value_counts().sort_index().例如:
mobile = pd.DataFrame({'PattLen':[1,1,2,6,6,7,7,7,7,8]})
print (mobile)
PattLen
0 1
1 1
2 2
3 6
4 6
5 7
6 7
7 7
8 7
9 8
print (mobile.PattLen.value_counts())
7 4
6 2
1 2
8 1
2 1
Name: PattLen, dtype: int64
mt = mobile.PattLen.value_counts().sort_index()
print (mt)
1 2
2 1
6 2
7 4
8 1
Name: PattLen, dtype: int64
Run Code Online (Sandbox Code Playgroud)
正如 jezrael\xe2\x80\x99s 答案\xe2\x80\xaf 下的normanius\xe2\x80\x99 评论所暗示的:
\n>>> df = pd.DataFrame({"a":[1,1,2,6,6,7,7,7,7,8]})\n>>> df.a.value_counts()[df.a.unique()]\n1 2\n2 1\n6 2\n7 4\n8 1\nName: a, dtype: int64\nRun Code Online (Sandbox Code Playgroud)\n通过显式提供自定义索引,可以按任何顺序排序\xe2\x80\xaf:
\n>>> df.a.value_counts()[[8,7,6,2,1]]\n8 1\n7 4\n6 2\n2 1\n1 2\nName: a, dtype: int64\n>>> df.a.value_counts()[[1,8,6,2,7]]\n1 2\n8 1\n6 2\n2 1\n7 4\nName: a, dtype: int64\nRun Code Online (Sandbox Code Playgroud)\n这对于绘制分类数据特别有用:
\n>>> df.a.value_counts()[['hourly','daily','weekly','monthly']].plot(type="bar")\nRun Code Online (Sandbox Code Playgroud)\n有趣的是,它可用于删除某些条目或使其他条目多次出现\xe2\x80\xaf:
\n>>> df.a.value_counts()[[1,1,1,8]]\n1 2\n1 2\n1 2\n8 1\nName: a, dtype: int64\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
26880 次 |
| 最近记录: |