简体   繁体   English

熊猫用用户定义的箱子切割

[英]Pandas cut with user-defined bins

I am working in Python and using the cut functionality in Pandas. 我正在使用Python并在Pandas中使用剪切功能。 I would like to have the bins in my pd.cut to be based on user-defined comma separated integers, with predefined top and bottom bounds. 我想让我的pd.cut中的bin基于用户定义的逗号分隔整数,具有预定义的顶部和底部边界。 In other words, I would like to predefine bins as [0, 100000] and then append user defined integers to this set of numbers. 换句话说,我想预定义bins[0, 100000]然后追加用户定义的整数这一组数字。 If the user entered 5, 100, 5000 then bins would become [0, 5, 100, 5000, 100000] and would then work with pd.cut(df['Quantity'], bins . 如果用户输入pd.cut(df['Quantity'], bins 5, 100, 5000那么bins将变为[0, 5, 100, 5000, 100000] pd.cut(df['Quantity'], bins [0, 5, 100, 5000, 100000] ,然后将与pd.cut(df['Quantity'], bins

I have tried a number of solutions with no avail up to this point - any input would be greatly appreciated! 我已经尝试了许多解决方案,但到目前为止无济于事 - 任何输入都将非常感谢!

Something like this? 像这样的东西?

bin_range = [0, 100000]
bin_input = raw_input("Enter bins as comma seperated list")
bin_input_parsed = [int(x.strip()) for x in bin_input.split(',')]

bins = [bin_range[0]] + bin_input_parsed + [bin_range[1]]
pd.cut(df['Quantity'], bins)

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

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