繁体   English   中英

是否有 python 或 pandas function 将列表切割成大小相等或尽可能对称的组?

[英]Is there a python or pandas function to cut a list into groups that are equal sized or as symmetric as possible?

我想要一个功能类似于pandas.qcut但是,它给了我尽可能“平衡”或“对称”的分裂。

目前,如果我使用:

pd.qcut(range(1, 11), 3, labels=False, duplicates="drop")

我得到:

array([0, 0, 0, 0, 1, 1, 1, 2, 2, 2], dtype=int64)

但我希望中间组有四个条目,即:

array([0, 0, 0, 1, 1, 1, 1, 2, 2, 2], dtype=int64).

Integer 算术可以做到。

import pandas as pd
pd.Series([i%3 for i in range(1, 11)]).sort_values().values

output

array([0, 0, 0, 1, 1, 1, 1, 2, 2, 2])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM