Python 数据帧按行数创建任意存储箱

Python 数据帧按行数创建任意存储箱,python,pandas,Python,Pandas,我现在有一个大约4000行的DF,我已经把它放入了我需要的顺序中 我需要按行数将DF划分为任意10个存储箱。因此,我想将bin编号附加到行中,以便将来按bin进行聚合计算。假设最后一个bin不会被填充,每个bin大约400 我现在不想在一个操作中执行此操作,我只需要将bin编号附加到行中。这样,我可以在另一个步骤中通过聚合进行分组 我把pd.qcut和pd.cut都打成平局,但似乎运气不好 ccs_category sex race marital_status presence_

我现在有一个大约4000行的DF,我已经把它放入了我需要的顺序中

我需要按行数将DF划分为任意10个存储箱。因此,我想将bin编号附加到行中,以便将来按bin进行聚合计算。假设最后一个bin不会被填充,每个bin大约400

我现在不想在一个操作中执行此操作,我只需要将bin编号附加到行中。这样,我可以在另一个步骤中通过聚合进行分组

我把pd.qcut和pd.cut都打成平局,但似乎运气不好

ccs_category    sex race    marital_status  presence_of_child   payer_type  no_of_persons   occupation  education   wealth_rate donate_charity  zip age 48_prediction   count   cum_count   cum_accurate    cum_percent
 263    218 M   U   S   N   PK  999 99  0   99  U   60657.0 0   0.000538    1   1   0   0.0
2452    250 M   W   U   U   NK  0   99  0   99  U   8730.0  29  0.000404    1   2   0   0.0
2814    127 F   W   U   N   MK  2   8   2   9   Y   53051.0 75  0.000369    1   3   0   0.0
所以我希望最后一列是bin_nu,顺序标签为1-10 0-9就可以了。例如,第一个400标签=1,第二个400标签=2等

results.3['bin_nu']=pd.cut(results3,10,labels=False)
我用retbins=True尝试过,也没有结果 给我:

TypeError: '<=' not supported between instances of 'str' and 'int'

让我们从50行的随机数据帧开始:

df=pd.DataFramenp.random.randn50,4,columns=listABCD

您可以使用groupby对其进行sumply chunk并获取chunk:

for sub_df_index, sub_df in df.groupby(np.arange(len(df)) // 10):
    print(sub_df.head(10))
前三部分:

          A         B         C         D
0  0.113454  3.357840 -0.413755 -1.089784
1  0.800012  0.655826  0.688414  0.012480
2  0.604902 -0.332028  0.470119 -0.370570
3  0.661120  0.635879 -0.441816 -0.847047
4  0.836218  2.597254  1.029996  0.554012
          A         B         C         D
5 -0.236094  1.714750 -0.091074  0.182944
6  0.928875 -1.125854  0.493389  0.309107
7 -0.238064  1.566493 -0.244627  0.744391
8  0.041049  0.423166  1.020502 -0.467028
9  0.290232  2.119993 -0.174697  0.784637
           A         B         C         D
10 -0.600395  0.604698  0.220617  2.122293
11  0.717157 -0.067665 -1.150331 -0.683567
12  1.006764 -0.869975 -1.646339  0.632909
13  0.076679  0.262971  0.687525  0.195338
14 -0.582238  0.236346 -0.903972 -0.223720
现在,您不需要您提议的新标签列;但是,如果您坚持要使用它,只需将其插入到每个新的sub_df中即可

for sub_df_index, sub_df in df.groupby(np.arange(len(df)) // 5):
    sub_df["sub_index"] = sub_df_index
    print(sub_df.head(10))
输出:

         A         B         C         D  sub_index
0  0.113454  3.357840 -0.413755 -1.089784          0
1  0.800012  0.655826  0.688414  0.012480          0
2  0.604902 -0.332028  0.470119 -0.370570          0
3  0.661120  0.635879 -0.441816 -0.847047          0
4  0.836218  2.597254  1.029996  0.554012          0
          A         B         C         D  sub_index
5 -0.236094  1.714750 -0.091074  0.182944          1
6  0.928875 -1.125854  0.493389  0.309107          1
7 -0.238064  1.566493 -0.244627  0.744391          1
8  0.041049  0.423166  1.020502 -0.467028          1
9  0.290232  2.119993 -0.174697  0.784637          1
           A         B         C         D  sub_index
10 -0.600395  0.604698  0.220617  2.122293          2
11  0.717157 -0.067665 -1.150331 -0.683567          2
12  1.006764 -0.869975 -1.646339  0.632909          2
13  0.076679  0.262971  0.687525  0.195338          2
14 -0.582238  0.236346 -0.903972 -0.223720          2
           A         B         C         D  sub_index
0  -1.381390  0.523980  1.306372  0.000278          0
1  -0.425316  0.937133  0.627025 -0.439032          0
2  -0.443357  0.160292  0.450645 -0.366276          0
3  -2.222720 -1.768990 -0.067939  1.239722          0
4   2.039943  0.774243  0.108462  0.192314          0
5  -0.702514 -1.258634 -1.086802  1.151799          1
6   1.269017  1.115269 -0.417813  1.161220          1
7  -0.620205 -0.054393  0.431089  0.436805          1
8  -2.321976 -1.269446  0.927542 -0.069101          1
9   0.387243  0.055290  1.519623 -0.732410          1
10 -0.227690 -1.991782 -0.712146  0.003375          2
11 -1.396515 -0.074016 -1.141520 -0.226016          2
12 -0.430559  1.347512 -0.773859  1.016727          2
13  0.867294  0.924141 -0.484293 -0.666916          2
14 -0.224497  0.818024  1.057355  1.700363          2
15 -0.790723 -0.039521  1.529804 -0.415783          3
编辑: 如果你需要一个单独的df,就这么做吧

df["sub_index"] = np.arange(len(df)) // 5
输出:

         A         B         C         D  sub_index
0  0.113454  3.357840 -0.413755 -1.089784          0
1  0.800012  0.655826  0.688414  0.012480          0
2  0.604902 -0.332028  0.470119 -0.370570          0
3  0.661120  0.635879 -0.441816 -0.847047          0
4  0.836218  2.597254  1.029996  0.554012          0
          A         B         C         D  sub_index
5 -0.236094  1.714750 -0.091074  0.182944          1
6  0.928875 -1.125854  0.493389  0.309107          1
7 -0.238064  1.566493 -0.244627  0.744391          1
8  0.041049  0.423166  1.020502 -0.467028          1
9  0.290232  2.119993 -0.174697  0.784637          1
           A         B         C         D  sub_index
10 -0.600395  0.604698  0.220617  2.122293          2
11  0.717157 -0.067665 -1.150331 -0.683567          2
12  1.006764 -0.869975 -1.646339  0.632909          2
13  0.076679  0.262971  0.687525  0.195338          2
14 -0.582238  0.236346 -0.903972 -0.223720          2
           A         B         C         D  sub_index
0  -1.381390  0.523980  1.306372  0.000278          0
1  -0.425316  0.937133  0.627025 -0.439032          0
2  -0.443357  0.160292  0.450645 -0.366276          0
3  -2.222720 -1.768990 -0.067939  1.239722          0
4   2.039943  0.774243  0.108462  0.192314          0
5  -0.702514 -1.258634 -1.086802  1.151799          1
6   1.269017  1.115269 -0.417813  1.161220          1
7  -0.620205 -0.054393  0.431089  0.436805          1
8  -2.321976 -1.269446  0.927542 -0.069101          1
9   0.387243  0.055290  1.519623 -0.732410          1
10 -0.227690 -1.991782 -0.712146  0.003375          2
11 -1.396515 -0.074016 -1.141520 -0.226016          2
12 -0.430559  1.347512 -0.773859  1.016727          2
13  0.867294  0.924141 -0.484293 -0.666916          2
14 -0.224497  0.818024  1.057355  1.700363          2
15 -0.790723 -0.039521  1.529804 -0.415783          3

嘿,你能不能把你的一小段数据添加为dict?问题的一部分是它是df。。但是我加上了我想我可以把df重新连接起来?我现在真的想要一个df作为结果?接下来我要做的组和聚合,有点奇怪,非常接近:-但我没有得到正确的子索引数…我得到的是每个子索引10行,而不是大约400行?你做过df[sub_index]=np.arangelendf//400吗?我不能只用400,我需要把df长度分成10行,就像这样?类似这样的情况,但也不正确:results3[sub_index]=np.arangelentresults3//lenresults3//10因为我试图将DF分成10部分,所以这会导致3[sub_index]=np.arangelentresults3//lenresults3//10,然后是results3=results3[:-1]将底部的1去掉奇数