我得到了福利。熊猫减产:
0 (0, 20]
1 (0, 20]
2 (0, 20]
3 (0, 20]
4 (0, 20]
5 (0, 20]
6 (0, 20]
7 (0, 20]
8 (0, 20]
9 (0, 20]如何将(0,20)转换为0- 20?
我是这样做的:
.str.replace('(', '').str.replace(']', '').str.replace(',', ' -')有更好的方法吗?
发布于 2016-08-12 23:14:02
假设将输出分配给变量cut
cut.astype(str)移除括号
cut.astype(str).str.strip('()[]')发布于 2016-08-12 23:16:59
使用labels参数的pd.cut
pd.cut(df['some_col'], bins=[0,20,40,60], labels=['0-20', '20-40', '40-60']) 我不知道确切的pd.cut命令是什么样子,但是上面的代码应该让您很好地了解该做什么。
示例用法:
df = pd.DataFrame({'some_col': range(5, 56, 5)})
df['cut'] = pd.cut(df['some_col'], bins=[0,20,40,60], labels=['0-20','20-40','40-60'])示例输出:
some_col cut
0 5 0-20
1 10 0-20
2 15 0-20
3 20 0-20
4 25 20-40
5 30 20-40
6 35 20-40
7 40 20-40
8 45 40-60
9 50 40-60
10 55 40-60https://stackoverflow.com/questions/38927585
复制相似问题