【问题标题】:How to group by a range of values in pandas?如何按熊猫中的一系列值分组?
【发布时间】:2016-07-07 04:47:10
【问题描述】:

我有一个dataframe,我想按分类变量和一系列值对其进行分组。您可能会将其视为类似值的行(集群?)。例如:

df = pd.DataFrame({'symbol' : ['IP', 'IP', 'IP', 'IP', 'IP', 'IP', 'IP'],
                   'serie' : ['A', 'B', 'A', 'B', 'A', 'B', 'B'],
                   'strike' : [10, 10, 12, 13, 12, 13, 14],
                   'last' : [1, 2, 2.5, 3, 4.5, 5, 6],
                   'price' : [11, 11, 11, 11, 11, 11, 11],
                   'type' : ['call', 'put', 'put', 'put', 'call', 'put', 'call']})

如果我使用

grouped = df.groupby(['symbol', 'serie', 'strike'])

我已经解决了部分问题,但我想组合更接近的罢工值,例如 10 和 11、12 和 13 等等。优选在%范围内。

【问题讨论】:

  • 能否显示预期的输出?
  • 您首先需要一个明确定义的标准来对罢工值进行聚类/分组。
  • 我认为创建一个具有平衡 bin 的列并按新的 bin-column 与其他列分组应该可以工作。
  • 不完全清楚你想要分组什么以及如何分组。你能展示你想要的结果吗?

标签: python pandas


【解决方案1】:

在strike 的 bins 上执行 groupy()

使用pd.cut 创建罢工数据箱,然后按该信息分组:

# Create DataFrame
df = pd.DataFrame({
    'symbol' : ['IP', 'IP', 'IP', 'IP', 'IP', 'IP', 'IP'],
    'serie' : ['A', 'B', 'A', 'B', 'A', 'B', 'B'],
    'strike' : [10, 10, 12, 13, 12, 13, 14],
    'last' : [1, 2, 2.5, 3, 4.5, 5, 6],
    'price' : [11, 11, 11, 11, 11, 11, 11],
    'type' : ['call', 'put', 'put', 'put', 'call', 'put', 'call']
})
# Create Bins (example three bins across data)
df['strikebins'] = pd.cut(df['strike'], bins=3)

print 'Binned DataFrame:'
print df
print

# Group these DataFrame
grouped = df.groupby(['symbol', 'serie', 'strikebins'])

# Do something with groups for example
gp_sum = grouped.sum()

print 'Grouped Sum (for example):'
print gp_sum
print

Binned DataFrame:
   last  price serie  strike symbol  type        strikebins
0   1.0     11     A      10     IP  call   (9.996, 11.333]
1   2.0     11     B      10     IP   put   (9.996, 11.333]
2   2.5     11     A      12     IP   put  (11.333, 12.667]
3   3.0     11     B      13     IP   put      (12.667, 14]
4   4.5     11     A      12     IP  call  (11.333, 12.667]
5   5.0     11     B      13     IP   put      (12.667, 14]
6   6.0     11     B      14     IP  call      (12.667, 14]

Grouped Sum (for example):
                               last  price  strike
symbol serie strikebins                           
IP     A     (9.996, 11.333]      1     11      10
             (11.333, 12.667]     7     22      24
             (12.667, 14]       NaN    NaN     NaN
       B     (9.996, 11.333]      2     11      10
             (11.333, 12.667]   NaN    NaN     NaN
             (12.667, 14]        14     33      40

如果需要,您可以 drop() strike,或者将 strikebins 替换为范围的平均值...

【讨论】:

    【解决方案2】:

    我猜 OP 想按分类变量分组,然后是一个按间隔分箱的数值变量。在这种情况下,您可以使用np.digitize()。

    smallest = np.min(df['strike'])
    largest = np.max(df['strike'])
    num_edges = 3
    # np.digitize(input_array, bin_edges)
    ind = np.digitize(df['strike'], np.linspace(smallest, largest, num_edges))
    

    那么ind应该是

    array([1, 1, 2, 2, 2, 2, 3], dtype=int64)
    

    对应分箱

     [10, 10, 12, 13, 12, 13, 14]
    

    带 bin 边缘

    array([ 10.,  12.,  14.]) # == np.linspace(smallest, largest, num_edges)
    

    最后,按您想要的所有列进行分组,但要使用这个额外的 bin 列

    df['binned_strike'] = ind
    for grp in df.groupby(['symbol', 'serie', 'binned_strike']):
        print "group key"
        print grp[0]
        print "group content"
        print grp[1]
        print "============="
    

    这应该打印出来

    group key
    ('IP', 'A', 1)
    group content
       last  price serie  strike symbol  type  binned_strike
    0   1.0     11     A      10     IP  call              1
    =============
    group key
    ('IP', 'A', 2)
    group content
       last  price serie  strike symbol  type  binned_strike
    2   2.5     11     A      12     IP   put              2
    4   4.5     11     A      12     IP  call              2
    =============
    group key
    ('IP', 'B', 1)
    group content
       last  price serie  strike symbol type  binned_strike
    1   2.0     11     B      10     IP  put              1
    =============
    group key
    ('IP', 'B', 2)
    group content
       last  price serie  strike symbol type  binned_strike
    3   3.0     11     B      13     IP  put              2
    5   5.0     11     B      13     IP  put              2
    =============
    group key
    ('IP', 'B', 3)
    group content
       last  price serie  strike symbol  type  binned_strike
    6   6.0     11     B      14     IP  call              3
    =============
    

    【讨论】:

      猜你喜欢
      • 2016-02-02
      • 2022-11-10
      • 1970-01-01
      • 2017-07-08
      • 2021-05-07
      • 2021-12-23
      • 2018-11-10
      • 1970-01-01
      • 2021-11-18
      相关资源
      最近更新 更多