我有一个dataframe,它包含4列,对于每一列,我们必须做bucketing(将数据分布在8个bucket中),这样就可以迭代地为第一列和第二列进行bucketing,而不必手动指定列名
这是我正在尝试的代码
for col in df3.columns[0:]:
cb1 = np.linspace(min(col), max(col), 11)
df3.insert(2 ,'buckets',pd.cut(col, cb1, labels=np.arange(1, 11, 1)))
print(df3[col])
这里df3是示例数据集
苹果橙香蕉
5 2 6条
六、四、六
2 8 9号
4 7 0年
预期输出为
苹果橙香蕉桶
5 2 6 1 3 2
6 4 6 1 4
2 8 9 2 1 8
4 7 0 5 4 1
这里bucket列指定了与数据相关的bucket编号
因为输出是完全随机的,所以数据列和bucket nums之间没有相关性,所以在这种情况下应该分别生成bucket。你知道吗
如果希望桶的大小相等:
相关问题 更多 >
编程相关推荐