简体   繁体   English

我在python中使用熊猫我正在尝试将价格范围内的数据分组

[英]Using pandas in python I am trying to group data from price ranges

Here is the code I am running, It creates a bar plot but i would like to group together values within $5 of each other for each bar in the graph. 这是我正在运行的代码,它创建了一个条形图,但我想将图中每个条形图彼此之间的差值在$ 5之内。 The bar graph currently shows all 50 values as individual bars and makes the data nearly unreadable. 条形图当前将所有50个值显示为单独的条形,并使数据几乎不可读。 Is a histogram a better option? 直方图是更好的选择吗? Also, bdf is the bids and adf is the asks. 此外,bdf是出价,而adf是要价。

import gdax
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from gdax import *
from pandas import *
from numpy import *
s= 'sequence'
b= 'bids'
a= 'asks'
public_client = gdax.PublicClient()
o = public_client.get_product_order_book('BTC-USD', level=2)
df = pd.DataFrame(o)
bdf = pd.DataFrame(o[b],columns = ['price','size','null'], dtype='float')
adf = pd.DataFrame(o[b],columns = ['price','size','null'], dtype='float')

del bdf['null'] bdf.plot.bar(x='price', y='size')
plt.show() 
pause = input('pause')

Here is an example of the data I receive as a DataFrame object. 这是我作为DataFrame对象接收的数据的示例。

       price       size
0   11390.99  13.686618
1   11389.40   0.002000
2   11389.00   0.090700
3   11386.53   0.060000
4   11385.26   0.010000
5   11385.20   0.453700
6   11381.33   0.006257
7   11380.06   0.011100
8   11380.00   0.001000
9   11378.61   0.729421
10  11378.60   0.159554
11  11375.00   0.012971
12  11374.00   0.297197
13  11373.82   0.005000
14  11373.72   0.661006
15  11373.39   0.001758
16  11373.00   1.000000
17  11370.00   0.082399
18  11367.22   1.002000
19  11366.90   0.010000
20  11364.67   1.000000
21  11364.65   6.900000
22  11364.37   0.002000
23  11361.23   0.250000
24  11361.22   0.058760
25  11360.89   0.001760
26  11360.00   0.026000
27  11358.82   0.900000
28  11358.30   0.020000
29  11355.83   0.002000
30  11355.15   1.000000
31  11354.72   8.900000
32  11354.41   0.250000
33  11353.00   0.002000
34  11352.88   1.313130
35  11352.19   0.510000
36  11350.00   1.650228
37  11349.90   0.477500
38  11348.41   0.001762
39  11347.43   0.900000
40  11347.18   0.874096
41  11345.42   7.800000
42  11343.21   1.700000
43  11343.02   0.001754
44  11341.73   0.900000
45  11341.62   0.002000
46  11341.00   0.024900
47  11340.00   0.400830
48  11339.77   0.002946
49  11337.00   0.050000

Is pandas the best way to manipulate this data? 熊猫是处理这些数据的最佳方法吗?

You can using cut here 你可以在这里使用cut

df['bin']=pd.cut(df.price,bins=3)
df.groupby('bin')['size'].sum().plot(kind='bar')

在此处输入图片说明

Not sure if I understand correctly, but if you want to count number of bids with a $5 step, here is how you can do it: 不知道我是否理解正确,但是如果您想以5美元为一步来计算出价数量,可以按以下步骤操作:

> df["size"].groupby((df["price"]//5)*5).sum()
price
11335.0     0.052946
11340.0     3.029484
11345.0    10.053358
11350.0    12.625358
11355.0     1.922000
11360.0     8.238520
11365.0     1.012000
11370.0     2.047360
11375.0     0.901946
11380.0     0.018357
11385.0     0.616400
11390.0    13.686618
Name: size, dtype: float64

暂无
暂无

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

相关问题 Python:如何使用pandas查询另一个dataframe范围内的数据 - Python: How to query data from ranges in another dataframe using pandas 当我尝试从 python 中的 Pandas 数据框创建新列时,部分关键字匹配不起作用? - Partial keyword match not working when I am trying to create a new column from a pandas data frame in python? 我正在尝试使用 python 从该网站下载年度数据,但我不知道如何处理它? - I am trying to download the Yearly data from this website using python but i am not sure how to approach it? 一种高效(快速)的方法,可以根据从Python Pandas中另一个DataFrame获取的范围将一个DataFrame中的连续数据分组? - Efficient (fast) way to group continuous data in one DataFrame based on ranges taken from another DataFrame in Python Pandas? 如何使用pandas / numpy根据特定值范围对数据进行分组? - How can I group by data based on specific value ranges using pandas/numpy? 我试图从使用urllib的网站获取html数据但是对于某些网站我最终在python中使用了一些未知字符 - I am trying to get html data from a site using urllib but for some sites i am ending up with some unknown characters in python 我正在尝试使用 Excel 中的数据用 Python 填写 web 表格 - I am trying to fill out a web form with Python using Data from Excel 我正在尝试使用 Windows 10 上的 python 3.8.3、openpyxl 从单列中的几行传输数据 - I am trying to transfer data from a few rows in a single column using python 3.8.3, openpyxl, on Windows 10 我正在尝试使用 pdfminer 在 python 中将数据提取为 HTML 元素 - I am trying to extract data as HTML elements in python using pdfminer 我正在尝试从python网站提取数据 - I am trying to extract data from a website in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM