Pandas:需要计算 0 到 0.001 然后是 0.001 和 0.002 等之间的列的值的数量 [英] Pandas: need to count the number of values of a column between 0 and 0.001 then 0.001 and 0.002 etc

查看:49
本文介绍了Pandas:需要计算 0 到 0.001 然后是 0.001 和 0.002 等之间的列的值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我的代码是这样的:

My code so far looks like this:

conn = psycopg2.connect("dbname=monty user=postgres host=localhost password=postgres")
cur = conn.cursor()
cur.execute("SELECT * FROM binance.zrxeth_ob_indicators;")
row = cur.fetchall()
df = pd.DataFrame(row,columns=['timestamp', 'topAsk', 'topBid', 'CPA', 'midprice', 'CPB', 'spread', 'CPA%', 'CPB%'])
ranges = (0, 0.05, 0.1, 0.15 ,0.2, 0.25, 0.3, 0.35, 0.4)
all_onbservations = df['CPA%'].groupby(pd.cut(df['CPA%'], ranges)).count()

我可以将它们计算为特定范围,但不能计算增量范围(从 0 到 0.001,然后从 0.001 到 0.002 到无限)......有什么想法吗?

I can count them for a specific range but not for an incremental one (between 0 to 0.001 then 0.001 to 0.002 to infinite)... any idea?

推荐答案

对于一致分隔的组,可以使用楼层划分来构造一个grouper:

For consistently separated groups, you can use floor division to construct a grouper:

np.random.seed(0)

df = pd.DataFrame({'A': np.random.random(100) * 0.5})

step = 0.05
res = df.groupby(df['A'] // step).size()
res.index *= step

print(res)

A
0.00    12
0.05    13
0.10     9
0.15     7
0.20    10
0.25    11
0.30    15
0.35     8
0.40     6
0.45     9
dtype: int64

这篇关于Pandas:需要计算 0 到 0.001 然后是 0.001 和 0.002 等之间的列的值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆