我有一个数据帧如下:
index accountid transdate
0 116490 2018-10-01
1 116490 2018-07-01
2 116490 2018-09-01
3 116490 2018-08-01
4 123033 2018-10-01
5 123033 2018-07-01
6 123033 2018-09-01
7 123033 2018-08-01
8 114175 2018-10-01
9 114175 2018-07-01
10 114175 2018-09-01
11 114175 2018-08-01
12 112962 2018-10-01
13 112962 2018-07-01
14 112962 2018-09-01
15 112962 2018-08-01
我试图从每个accountid
组中随机获得行数。
例如,这里每个accountid有4个transdates
,我试图按accountid
分组,从每个组中获得最少1行和最多4行。你知道吗
预期产量:
index accountid transdate
0 116490 2018-10-01
1 116490 2018-07-01
3 116490 2018-08-01
4 123033 2018-10-01
5 123033 2018-07-01
8 114175 2018-10-01
9 114175 2018-07-01
10 114175 2018-09-01
11 114175 2018-08-01
12 112962 2018-10-01
13 112962 2018-07-01
15 112962 2018-08-01
我一直按accountid
分组,并对分组对象应用random.sample
,但每次它都会从每个组返回固定数量的行。你知道吗
您可以使用^{} 获得每个类别的随机样本,并且可以设置要在
1 ... min(4, len(category))
中随机分布的元素数:相关问题 更多 >
编程相关推荐