2024-04-24 22:07:01 发布
网友
给定一个特定列中包含数值的数据帧,我希望随机删除特定列中的值位于某个范围内的行的一定百分比。在
例如,给定以下数据帧:
df = pd.DataFrame({'col1': [1,2,3,4,5,6,7,8,9,10]}) df col1 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
col1低于6的行中有2/5应该被随机删除。在
最简洁的方法是什么?在
使用sample+drop
sample
drop
df.drop(df.query('col1 < 6').sample(frac=.4).index) col1 1 2 3 4 4 5 5 6 6 7 7 8 8 9 9 10
对于一个范围
使用
sample
+drop
对于一个范围
^{pr2}$相关问题 更多 >
编程相关推荐