[英]How can I dynamically create bins in Python?
I have the following np array: 我有以下np数组:
[['ID1', 922.63, 'Product 1'],
['ID1', 1001, 'Product 2'],
['ID1', 800, 'Product 1'],
['ID1', 922.63, 'Product 1'],
['ID1', 1001, 'Product 2'],
['ID2', 800, 'Product 1'],
['ID2', 922.63, 'Product 1'],
['ID2', 1001, 'Product 2'],
['ID3', 800, 'Product 1'],
['ID3', 700.63, 'Product 1'],
['ID3', 1200, 'Product 2'],
['ID3', 850, 'Product 1']]
The '2nd column' ($ amount) is what I care about. 我关心的是“第二列”($金额)。 I want to build a histogram of product 1 and product 2, but I want the bins to be sized by 100. The actual data set I'm using has 75K rows and values that range from $1 to $200000.
我想建立产品1和产品2的直方图,但我希望将容器的大小设置为100。我正在使用的实际数据集有75K行,其值的范围从$ 1到$ 200000。 I want to automatically create these 'buckets' for the values and then build a histogram.
我想自动为这些值创建这些“存储桶”,然后构建一个直方图。
I thought it would be easy to find info on this using either pandas or numpy but I am either a newb and not able to understand other 'similar' solutions, or am just not finding what I'm looking for. 我认为使用pandas或numpy可以很容易地找到有关此信息,但是我要么是新手,要么无法理解其他“相似”解决方案,或者只是找不到我想要的东西。 Seems like it should be straight forward.
似乎应该是直截了当的。
You can get a histogram by turning your data into a pandas.DataFrame
: 您可以通过将数据转换为
pandas.DataFrame
来获得直方图:
a = [['ID1', 922.63, 'Product 1'],
['ID1', 1001, 'Product 2'],
['ID1', 800, 'Product 1'],
['ID1', 922.63, 'Product 1'],
['ID1', 1001, 'Product 2'],
['ID2', 800, 'Product 1'],
['ID2', 922.63, 'Product 1'],
['ID2', 1001, 'Product 2'],
['ID3', 800, 'Product 1'],
['ID3', 700.63, 'Product 1'],
['ID3', 1200, 'Product 2'],
['ID3', 850, 'Product 1']]
q=pd.DataFrame(a,columns=['id','price','product'])
q.hist(column='price',bins=100)
You can specify the number of bins you want with the bins
parameter: 您可以使用
bins
参数指定所需的bins
数:
q.hist(column='price', bins=100)
If you want to group it by product use the by
parameter: 如果要按产品分组,请使用
by
参数:
q.hist(column='price', bins=100,by='product')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.