无法删除/删除数据帧中的行

2024-09-29 23:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,我尝试随机解析一个数据帧。然后根据生成的随机数,我想删除/删除该行,但将其放入另一个数据帧中。稍后,我想在提取的行和剩下的整个数据帧条目之间进行比较。但是,我似乎无法从数据帧中删除随机生成的索引行。每当我在打印语句出现后运行它时。知道我做错了什么吗

我已经尝试过data.drop(data.index[y]),但这不会删除任何行,因为在末尾打印时,我的数据框会显示所有行,看起来没有删除任何行

col_names = ['letter','m00', 'mu02', 'mu11', 'mu20', 'mu03', 'mu12', 'mu21', 'mu30']
data.head()
data = pd.read_csv('traindat.txt', delim_whitespace = True,skiprows = 1, header = None)
plucked_df = pd.DataFrame(columns = col_names)
data.columns = col_names
#df.loc[row,column] or df.loc[0,'mu00']
#print(data.loc[3,'m00'])
print(data)
x = len(data)
#df_marks = df_marks.append(new_row, ignore_index=True)
for i in range(0,x):
    y = random.randint(0,x)
    #print(y)
    print(data.loc[y])
    plucked_df = plucked_df.append(data.loc[y],ignore_index =True)
    #print(plucked_df)
    #data.drop([y])
    data.drop(data.index[y])
    print(data.index[y])
    #del data[y]
    data.reset_index(drop=True,inplace = True)
    x = len(data) -1
    #print(data)
#print(len(data))
print("data",data)
print("pluck",plucked_df)

Tags: columns数据truedfdataindexlennames

热门问题