【发布时间】:2019-03-02 13:08:03
【问题描述】:
我有一个约 3700 行的数据集,需要根据列删除其中的 1628 行。数据集如下所示:
compliance day0 day1 day2 day3 day4
True 1 3 9 8 8
False 7 4 8 3 2
True 4 5 0 3 5
True 5 3 9 6 2
对于 1068 行,如果合规性 = true,我想删除整行。
问题是,我想随机执行此操作;我不想删除前 1063 行。我试过这个:
for z in range(1629):
rand = random.randint(0,(3783-z)) #subtract z since dataframe shape is shrinking
if str(data.iloc[rand,1]) == 'True':
data = data.drop(balanced_dataset.index[rand])
但在删除几行后,我收到以下错误:
'labels [2359] not contained in axis'
我也试过这个:
data.drop(data("adherence.str.startswith('T').values").sample(frac=.4).index)
frac 现在是任意选择的,我只是想让它工作。我收到以下错误:
'DataFrame' object is not callable
任何帮助将不胜感激!谢谢
【问题讨论】:
-
我还发现它一直有效,直到随机数两次给出相同的数字
标签: python pandas dataframe random