需要将一列划分为三个不同的仓a-B-C,每个仓中的观测数量相同

  • 本文关键字:a-B-C 三个 一列 划分 python pandas
  • 更新时间 :
  • 英文 :


我的数据帧看起来是这样的:

timestamp              topAsk       topBid     CPA          midprice    CPB      spread   gamma_perc
72554   2018-11-17 18:43:00 0.00307084  0.00306366  0.00307085  0.00306725  0.00306725  0.00000718  0.000000
35867   2018-10-23 02:06:00 0.00445542  0.00444528  0.00445542  0.00445035  0.00445035  0.00001014  0.000000
65021   2018-11-12 10:28:00 0.00327366  0.00326954  0.00327160  0.00327160  0.00327160  0.00000412  0.000000
65020   2018-11-12 10:27:00 0.00327246  0.00326834  0.00327100  0.00327040  0.00327040  0.00000412  0.000000
65017   2018-11-12 10:24:00 0.00327756  0.00327341  0.00327548  0.00327548  0.00327548  0.00000415  0.000000
35872   2018-10-23 02:11:00 0.00445192  0.00444249  0.00445192  0.00444721  0.00444721  0.00000943  0.000000
65016   2018-11-12 10:23:00 0.00327756  0.00327341  0.00327548  0.00327548  0.00327548  0.00000415  0.000000
65015   2018-11-12 10:22:00 0.00327756  0.00327341  0.00327548  0.00327548  0.00327548  0.00000415  0.000000
65014   2018-11-12 10:21:00 0.00327756  0.00327341  0.00327548  0.00327548  0.00327548  0.00000415  0.000000
65013   2018-11-12 10:20:00 0.00327756  0.00327341  0.00327548  0.00327548  0.00327548  0.00000415  0.000000
... ... ... ... ... ... ... ... ...
82213   2018-11-24 11:43:00 0.00324989  0.00324561  0.00325211  0.00324775  0.00324114  0.00000428  154.439252
88427   2018-11-28 19:17:00 0.00342308  0.00341001  0.00342256  0.00341654  0.00339635  0.00001307  154.475899
63023   2018-11-11 01:10:00 0.00336728  0.00336673  0.00336701  0.00336701  0.00336616  5.5E-7  154.545455
17294   2018-10-10 04:32:00 0.00334544  0.00333056  0.00333802  0.00333800  0.00331500  0.00001488  154.569892
34890   2018-10-22 09:49:00 0.00437069  0.00436719  0.00436894  0.00436894  0.00436353  0.00000350  154.571429
30957   2018-10-19 16:16:00 0.00438949  0.00438403  0.00439011  0.00438676  0.00437832  0.00000546  154.578755
23556   2018-10-14 12:55:00 0.00371373  0.00370981  0.00371279  0.00371177  0.00370571  0.00000392  154.591837
38583   2018-10-24 23:22:00 0.00417979  0.00417406  0.00417915  0.00417692  0.00416806  0.00000573  154.624782
62668   2018-11-10 19:15:00 0.00339415  0.00339102  0.00339259  0.00339259  0.00338775  0.00000313  154.632588

我需要做的是添加一个新列pread_bin,并将排列的观测值排序到大小相等的仓(a、B和C)中。到目前为止,我尝试的是对数据帧进行排序,并将它们切割成3个数组,这将是我的垃圾箱,就像这样:

df_new_sample = df_new_sample.sort_values(by='spread')
sorted_array = np.sort(df_new_sample['spread'])
split_spreads = np.array_split(sorted_array, 3)
df_new_sample['spread_bin'] = df_new_sample['spread'].apply(lambda x: 'A' if x <= split_spreads[0][-1] else ( 'B' if split_spreads[0][-1] < x <= split_spreads[1][-1] else 'C'))
spread                bin
39478          1E-8   A
42804          1E-8   A
42411          1E-8   A
21897          1E-8   A
27103          1E-8   A
51190          1E-8   A
42452          1E-8   A
42288          1E-8   A
717            1E-8   A
23948          1E-8   A
...    
68148    0.00004299   C
76725    0.00004568   C
19495    0.00004706   C
19530    0.00004737   C
77057    0.00004761   C
17368    0.00005202   C
24590    0.00005365   C
19528    0.00006249   C
19489    0.00007012   C
19484    0.00011030   C

但当我仔细检查每个垃圾箱中是否有相同数量的观察结果时,我会有差异。。。怎么会这样?

您看过qcut吗?

df_new_sample['spread_bin'] = pd.qcut(df_new_sample['spread'], 3, labels=['A', 'B', 'C'])

您的数据集的边缘恰好有重复数据,因此,不可能将数据集放入大小相等的桶中。但是,如果您不关心spread值是否渗入另一个bin,则可以人为地创建一个排名列。

# first sort your df by `spread`
df_new_sample = df_new_sample.sort_values('spread')
# reset index
df_new_sample = df_new_sample.reset_index(drop=True)
# now qcut on the index
df_new_sample['spread_bin'] = pd.qcut(df_new_sample.index, 3, labels=['A', 'B', 'C']

注意:如果您希望每个bin中有相同数量的观测值,则df中的观测值必须可以被3整除。

Pandas有一个内置的函数cut来实现这一点:

df_new_sample['spread_bin'] = pd.cut(df_new_sample['spread'], 3)

最新更新