【问题标题】:Pandas: need to count the number of values of a column between 0 and 0.001 then 0.001 and 0.002 etcPandas:需要计算 0 到 0.001 之间的列值的数量,然后是 0.001 和 0.002 等
【发布时间】:2019-05-26 11:37:49
【问题描述】:

到目前为止,我的代码如下所示:

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 到无限)...有什么想法吗?

【问题讨论】:

  • 这听起来像是分箱,但我没有足够的知识来建议相应的答案

标签: python pandas pandas-groupby


【解决方案1】:

对于始终分隔的组,您可以使用地板划分来构造一个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 2014-05-25
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多